1 CancellationTokenSource _cancelSource= new CancellationTokenSource(); 2 CancellationToken _cancelToken= tokenSource.Token; 3 ManualResetEvent _resetEvent = new ManualResetEvent(true);
Task task = new Task(async () => { while (true) { if (_cancelToken.IsCancellationRequested) { return; } // 初始化为true时执行WaitOne不阻塞 _resetEvent.WaitOne(); // Doing something....... } }, token); task.Start();
3.暂停Task
_resetEvent.Reset();
4.继续Task
_resetEvent.Set();
5.取消Task
_cancelSource.Cancel();
备注:任务取消后如果想重开任务,不能使用已经取消的token,需要重新声明一个对象.
来源:U5网赚博客(QQ/微信:313919848),转载请保留出处和链接!
本文链接: https://www.u5fuye.com/post/56638
相关阅读:
Visual Studio 中设置 JavaScript/TypeScript 的断点 脚本出现
本文暂时没有评论,来添加一个吧(●'◡'●)