Home > Net >  CancellationToken and Task
CancellationToken and Task

Time:11-06

 
Var tokenSource=new CancellationTokenSource ();
Var token=tokenSource. Token;
Task. Run (()=& gt;
{
While (true)
{
Token. ThrowIfCancellationRequested ();
Console. WriteLine ($" [1] {DateTime. Now: mm: ss. The FFF} ");
Thread.sleep (1000);
}
}, token);
Task. Run (()=& gt;
{
Thread.sleep (5000);
Console. WriteLine ($" [2] {DateTime. Now: mm: ss. The FFF} ");
TokenSource. Cancel ();
});


In Task in the Run method, the same effect without the second parameter CancellationToken,
To cancel or there is a "token ThrowIfCancellationRequested ();" ,


Is
Task. The Run (action)
And
Task. The Run (action, token)
What's the difference?

Want to know the Task. What is the function of the Run of the second argument? Usually used in how scenarios?


  •  Tags:  
  • C#
  • Related