Guest User

Untitled

a guest
Jan 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. class Program
  2. {
  3. static async Task Main(string[] args)
  4. {
  5. var cts = new CancellationTokenSource();
  6. var task = DoSomeWork(cts.Token);
  7. await Task.Delay(2000);
  8. cts.Cancel();
  9. await Task.Delay(2000);
  10. WriteLine("Yep, we done");
  11. ReadLine();
  12. }
  13.  
  14. private async static Task DoSomeWork(CancellationToken token)
  15. {
  16. try
  17. {
  18. while(true)
  19. {
  20. await Task.Delay(1000);
  21. token.ThrowIfCancellationRequested();
  22. }
  23. }
  24. catch(OperationCanceledException)
  25. {
  26. WriteLine("Task cancelled!");
  27. }
  28. catch(Exception)
  29. {
  30. WriteLine("wth?");
  31. }
  32.  
  33. }
  34. }
Add Comment
Please, Sign In to add comment