Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- namespace TPL_part_1_cancelling_task
- {
- class Program
- {
- static void Main(string[] args)
- {
- //creating the cancelation token
- CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
- CancellationToken token = cancellationTokenSource.Token;
- //creating the task
- Task task = new Task(() =>
- {
- for (int i = 0; i < 100000; i++)
- {
- if (token.IsCancellationRequested)
- {
- Console.WriteLine("Cancel() called.");
- return;
- }
- Console.WriteLine("Loop value {0}", i);
- }
- }, token);
- Console.WriteLine("Press any key to start task");
- Console.WriteLine("Press any key again to cancel the running task");
- Console.ReadKey();
- //starting the task
- task.Start();
- //reading a console key
- Console.ReadKey();
- //canceling the task
- Console.WriteLine("Canceling task");
- cancellationTokenSource.Cancel();
- Console.WriteLine("Main method complete. Press any key to finish.");
- Console.ReadKey();
- }
- }
- }
- // http://www.c-sharpcorner.com/UploadFile/f9f215/parallel-programming-part-1-introducing-task-programming-l/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement