Guest User

Untitled

a guest
Nov 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. namespace manage_multithreading
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
  8. CancellationToken token = cancellationTokenSource.Token;
  9. int theCount = 0;
  10. Console.WriteLine("Task running...Count from 0 to...");
  11. Console.WriteLine("Press ENTER to interrupt the execution!");
  12. Task theIncredibleTask = Task.Run(() =>
  13. {
  14. while (!token.IsCancellationRequested)
  15. {
  16. Thread.Sleep(1000);
  17. Console.WriteLine(theCount.ToString());
  18. theCount++;
  19. }
  20. }, token);
  21.  
  22. Console.ReadLine();
  23. cancellationTokenSource.Cancel();
  24.  
  25. Console.WriteLine("Execution cancelled!");
  26. Console.ReadLine();
  27. }
  28. }
  29. }
Add Comment
Please, Sign In to add comment