Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static async Task Main(string[] args)
- {
- var cts = new CancellationTokenSource();
- var task = DoSomeWork(cts.Token);
- await Task.Delay(2000);
- cts.Cancel();
- await Task.Delay(2000);
- WriteLine("Yep, we done");
- ReadLine();
- }
- private async static Task DoSomeWork(CancellationToken token)
- {
- try
- {
- while(true)
- {
- await Task.Delay(1000);
- token.ThrowIfCancellationRequested();
- }
- }
- catch(OperationCanceledException)
- {
- WriteLine("Task cancelled!");
- }
- catch(Exception)
- {
- WriteLine("wth?");
- }
- }
- }
Add Comment
Please, Sign In to add comment