Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 的基本使用 5 #23

Open
Henrik-Xu opened this issue May 27, 2019 · 0 comments
Open

Task 的基本使用 5 #23

Henrik-Xu opened this issue May 27, 2019 · 0 comments

Comments

@Henrik-Xu
Copy link
Owner

Henrik-Xu commented May 27, 2019

Task 的基本使用 5

Task 中的取消功能:使用的是 CacellationTokenSoure 解决多任务中协作取消和超时取消方法

1.简单的线程的取消(存在 bug)

var isStop = false;//标志变量
var thread = new Thread(() =>
{
  while (!isStop)
  {
      Thread.Sleep(200);
      Console.WriteLine("threadId=" + Thread.CurrentThread.ManagedThreadId);
  }
});
thread.Start();

//主线程的任务
Thread.Sleep(1000);
isStop = true;//不能让多个线程操作一个共享变量,否则在release版本中有潜在的bug

2.Task任务的取消与判断:CancellationTokenSource

实现和前面isStop判断相同的功能,去处理"取消任务” 但是比前面优化很多....

//前面用过的类:
CancellationTokenSource cts = new CancellationTokenSource();

Task task = Task.Factory.StartNew(() =>
{
  while (!cts.IsCancellationRequested)//判断任务是否被取消
  {
    Thread.Sleep(200);
    Console.WriteLine("threadId=" + Thread.CurrentThread.ManagedThreadId);
  }
}, cts.Token);

Thread.Sleep(1000);
cts.Cancel();

3.Task任务取消的时候,我们希望能够有一些其他的清理工作要执行,也就是这个取消的动作会触发一

个任务,比如更新订单队列,或数据库等

CancellationTokenSource cts = new CancellationTokenSource();

//注册一个委托:这个委托将在任务取消的时候调用
cts.Token.Register(() =>
{
  //在这个地方可以编写自己要处理的逻辑....

  Console.WriteLine("当前task被取消,我们现在可以做相关的清理工作...");
});

Task task = Task.Factory.StartNew(() =>
{
  while (!cts.IsCancellationRequested)
  {
    Thread.Sleep(200);
    Console.WriteLine("threadId=" + Thread.CurrentThread.ManagedThreadId);
  }
}, cts.Token);

Thread.Sleep(1000);
cts.Cancel();

4.Task任务延时取消:比如我们请求远程的接口,如果在指定时间没有返回数据,我们可以做一个时间

限制,超时可以取消任务

//CancellationTokenSource cts = new CancellationTokenSource();
CancellationTokenSource cts = new CancellationTokenSource(2000);

//注册一个委托:这个委托将在任务取消的时候调用
cts.Token.Register(() =>
{
  //在这个地方可以编写自己要处理的逻辑....

  Console.WriteLine("当前task被取消,我们现在可以做相关的清理工作...");
});

Task task = Task.Factory.StartNew(() =>
{
  while (!cts.IsCancellationRequested)
  {
    Thread.Sleep(200);
    Console.WriteLine("threadId=" + Thread.CurrentThread.ManagedThreadId);
  }
}, cts.Token);

////取消方法1:public void CancelAfter(TimeSpan delay);
//cts.CancelAfter(new TimeSpan(0, 0, 0, 2));

//取消方法2:public void CancelAfter(int millisecondsDelay);
//在构造方法中设置取消的时间
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant