Last active
November 18, 2019 11:48
-
-
Save cilliemalan/17e00434695a3ab044f4b0da6b594ab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBackgroundService : BackgroundService | |
{ | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
// run until stoppingToken is cancelled??? | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBackgroundService : BackgroundService | |
{ | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
while (!stoppingToken.IsCancellationRequested) | |
{ | |
await DoWorkAsync(stoppingToken); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBackgroundService : BackgroundService | |
{ | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
using var disposer = messageQueue.Subscribe(async (msg) => | |
{ | |
await ProcessMessageAsync(msg, stoppingToken); | |
}); | |
// Wait until the service is stopped | |
await stoppingToken; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Core | |
{ | |
public static class AsyncExtensions | |
{ | |
/// <summary> | |
/// Allows a cancellation token to be awaited. | |
/// </summary> | |
[EditorBrowsable(EditorBrowsableState.Never)] | |
public static CancellationTokenAwaiter GetAwaiter(this CancellationToken ct) | |
{ | |
// return our special awaiter | |
return new CancellationTokenAwaiter | |
{ | |
CancellationToken = ct | |
}; | |
} | |
/// <summary> | |
/// The awaiter for cancellation tokens. | |
/// </summary> | |
[EditorBrowsable(EditorBrowsableState.Never)] | |
public struct CancellationTokenAwaiter : INotifyCompletion, ICriticalNotifyCompletion | |
{ | |
public CancellationTokenAwaiter(CancellationToken cancellationToken) | |
{ | |
CancellationToken = cancellationToken; | |
} | |
internal CancellationToken CancellationToken; | |
public object GetResult() | |
{ | |
// this is called by compiler generated methods when the | |
// task has completed. Instead of returning a result, we | |
// just throw an exception. | |
if (IsCompleted) throw new OperationCanceledException(); | |
else throw new InvalidOperationException("The cancellation token has not yet been cancelled."); | |
} | |
// called by compiler generated/.net internals to check | |
// if the task has completed. | |
public bool IsCompleted => CancellationToken.IsCancellationRequested; | |
// The compiler will generate stuff that hooks in | |
// here. We hook those methods directly into the | |
// cancellation token. | |
public void OnCompleted(Action continuation) => | |
CancellationToken.Register(continuation); | |
public void UnsafeOnCompleted(Action continuation) => | |
CancellationToken.Register(continuation); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBackgroundService : BackgroundService | |
{ | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
using var disposer = messageQueue.Subscribe(async (msg) => | |
{ | |
await ProcessMessageAsync(msg, stoppingToken); | |
}); | |
// Nasty! | |
while (!stoppingToken.IsCancellationRequested) | |
{ | |
await Task.Delay(1000000, stoppingToken); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment