Cancelling

Tasks use background threads from the thread pool.
To cancel a task you need to pass a special cancellation token to the task factory.


System.Threading.Cancellation.TokenSource tokenSource = new System.Threading.CancellationTokenSource(); 
System.Threading.Task.Factory.StartNew( () => System.Console.WriteLine("my new task"), tokenSource.token);
System.Threading.Thread.Sleep(2500);
tokenSource.Cancel();


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext