We usually use a similar code in multi-threading bool IsExit to control whether the thread runs and terminates, in fact, using CancellationTokenSource to control more useful, we will introduce CancellationTokenSource related usage.
C # uses CancellationTokenSource to terminate threads
Using the CancellationTokenSource object needs to be used in conjunction with the Task object, and the task will control the state of the current run (this does not concern us with how the hole is controlled). The CancellationTokenSource is the external control of the task, such as cancellation and timing cancellation.
Let's take a look at the sample code
- class Program
- {
- //Declare CancellationTokenSource object
- static cancellationtokensource canceltokensource = new CancellationTokenSource();
- //Program Entry
- static void Main(string[] args)
- {
- Task. Factory. StartNew(mytask, canceltokensource. Token);
- Console. WriteLine("Please press ENTER to stop");
- Console. ReadLine();
- Canceltokensource. Cancel();
- Console. WriteLine("stopped");
- Console. ReadLine();
- }
- //test method
- static void mytask()
- {
- //Decide whether to cancel the task
- while (! Canceltokensource. iscancellationrequested)
- {
- Console. WriteLine(DateTime. Now);
- Thread. Sleep(+);
- }
- }
- }
Run effect
Task.Factory.StartNew creates and launches the MyTask method, and passes a Cancellationtokensource.token object in. We can suppress the operation of the task by controlling the external CancellationTokenSource object.
When the canceltokensource.iscancellationrequested in the MyTask determines if the task is canceled, it jumps out of the while loop to execute. And it ended the mission.
We can also use the timed cancel task, when a task exceeds the time we set and then automatically cancels the execution of the task. As shown in the following code
- var canceltokensource = new CancellationTokenSource(+);
In addition to constructors, we can use a different way to implement timed cancellations, as shown in the following code
- Canceltokensource. Cancelafter(+);
The effect is the same, 3 seconds to cancel the timer.
Multiple CancellationTokenSource composite
When there are multiple cancellationtokensource that need to be managed concurrently, such as any one of the tasks canceled, all tasks are canceled. We don't have to go to one to close, just combine the cancellationtokensource that need to be closed together in parallel. As shown in the following code, the execution results are the same as in the above diagram. I will not be anymore.
- class Program
- {
- //Declare CancellationTokenSource object
- static cancellationtokensource C1 = new CancellationTokenSource();
- static cancellationtokensource c2 = new CancellationTokenSource();
- static CancellationTokenSource c3 = new CancellationTokenSource() ;
- //Use multiple CancellationTokenSource for composite management
- static cancellationtokensource compositecancel = cancellationtokensource. Createlinkedtokensource(C1. Token, C2. Token, c3. Token);
- //Program Entry
- static void Main(string[] args)
- {
- Task. Factory. StartNew(mytask, compositecancel. Token);
- Console. WriteLine("Please press ENTER to stop");
- Console. ReadLine();
- //Any one CancellationTokenSource cancel the task, then all tasks will be canceled.
- C1. Cancel();
- Console. WriteLine("stopped");
- Console. ReadLine();
- }
- //test method
- static void mytask()
- {
- //Decide whether to cancel the task
- while (! Compositecancel. iscancellationrequested)
- {
- Console. WriteLine(DateTime. Now);
- Thread. Sleep(+);
- }
- }
- }
The above code calls the c1.Cancel(); trigger MyTask() in the method compositeCancel.IsCancellationRequested to True, then cancels the task. So we judge the CancellationTokenSource object of the compound in all the tasks.
Reprinted from: http://www.wxzzz.com/689.html
C # cancellationtokensource terminating thread