C#协作试取消线程
https://segmentfault.com/q/1010000017109927
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace 取消线程池中线程
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine( "主线程运行" );
CancellationTokenSource cts = new CancellationTokenSource( );
//ThreadPool.QueueUserWorkItem(CallBack, cts.Token );
ThreadPool.QueueUserWorkItem(CallBack, cts);
Console.WriteLine( "按下回车键取消启动的线程" );
Console.Read( );
cts.Cancel();
Console.ReadKey();
}
public static void CallBack( object state )
{
CancellationTokenSource cts = (CancellationTokenSource)state;
Console.WriteLine( "开始计数" );
Count( cts, 5 );
}
private static void Count(CancellationTokenSource cts, int countto )
{
for( int i=0; i<=countto+10; ++i) {
if( i== countto) {
cts.Cancel();
Console.WriteLine( "计算取消" );
//return;
}
Console.WriteLine( "计算为:{0}, 当前线程ID: {1}", i, Thread.CurrentThread.ManagedThreadId );
Thread.Sleep(300);
}
Console.WriteLine("计数完成");
}
}
}
很明显在调用cts.Cancel()后Count()函数还在执行,看到书上说CancellationTokenSource是协助式取消线程,刚学多线程,很多不明白的。
我们将一些任务(函数)放在某一个线程中执行,如果任务终止是不是就代表线程取消了
上述代码如果改为:
if( i== countto) {
cts.Cancel();
Console.WriteLine( "计算取消" );
return;
}
这样是不是就可以正确的取消线程。
在 Count 方法中需要使用 cts.IsCancellationRequested 来判断当前线程是否已经被取消。
if (cts.IsCancellationRequested)
{
Console.WriteLine( "计算取消" );
return;
}
C#协作试取消线程的更多相关文章
- Linux学习笔记23——取消线程
一 相关函数 1 发送终止信号 #include <pthread.h> int pthread_cancel(pthread_t thread); 2 设置取消状态 #include & ...
- pthread中取消线程
取消线程:告诉一个线程关掉自己,取消操作允许线程请求终止其所在进程中的任何其他线程.不希望或不需要对一组相关的线程执行进一步操作时,可以选择执行取消操作.取消线程的一个示例是异步生成取消条件. 对于c ...
- linux下pthread_cancel无法取消线程的原因【转】
转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...
- linux下pthread_cancel无法取消线程的原因
一个线程能够调用pthread_cancel终止同一进程中的还有一个线程,可是值得强调的是:同一进程的线程间,pthread_cancel向还有一线程发终止信号.系统并不会立即关闭被取消线程,仅仅有在 ...
- 【C#】线程协作式取消
Microsoft .Net Framework 提供了一个标准的取消操作的模式.这个模式是协作式的,意味着你想取消的操作必须显示地支持取消. CLR为我们提供了两个类: System.Threadi ...
- Linux 进程与线程二(等待--分离--取消线程)
int pthread_join(pthread_t thr,void **thr_return); pthread_join函数用于挂起当前线程,直至th指定的线程终止为止. 如果另一个线程返回值不 ...
- CancellationTokenSource 和 CancellationToken 取消线程
Main 程序[分别调用三个方法] static void Main(string[] args) { using (CancellationTokenSource cts = new Cancell ...
- 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...
- day4 liaoxuefeng--调试、线程、正则表达式
一.错误.调试和测试 二.进程和线程 三.正则表达式
随机推荐
- 公钥-私钥 白名单-黑名单 Linux 远程访问及控制(SSH)
远程访问及控制一.SSH远程管理二.OpenSSH服务器① SSH (Secure Shell)协议② OpenSSH三.配置OpenSSH服务器举例四.sshd 服务支持两种验证方式五.使用SSH客 ...
- C语言:外部函数
//main.c #include <stdio.h> extern void func(); extern int m; int n = 200; int main(){ func(); ...
- python 交换变量的值 不需要借助第三个变量
>>> a,b,c,d=1,2,3,4>>> a,b,c,d=d,c,b,a>>> print(a,b,c,d)4 3 2 1>>&g ...
- ffmpeg入门篇-滤镜的基本使用
转发自白狼栈:查看原文 滤镜 什么是滤镜?百度百科介绍说"滤镜主要是用来实现图像的各种特殊效果......". 我们最早在ffmpeg是如何转码的一文中了解过滤镜,来回顾下当时的转 ...
- [iconfont_dart]帮你快速生成Icon,再也不用手动写Icon方法
iconfont_dart iconfont to dart.Icon can be implemented by calling iconfont classname. iconfont转dart. ...
- 前端开发入门到进阶第三集【js和jquery的执行时间与页面加载的关系】
https://blog.csdn.net/u014179029/article/details/81603561 [原文链接]:https://www.cnblogs.com/eric-qin/p/ ...
- springMVC-4-处理模型数据
返回模型数据(Model) index.jsp中 <h1>获取模型数据</h1> <a href="/model/test1">ModelAnd ...
- R语言客户端RStudio快捷键大全
Console Description Windows & Linux Mac 将光标定位到控制台 Ctrl+2 Ctrl+2 清空控制台 Ctrl+L Command+L 将光标定位到行首 ...
- Spring Security OAuth2 远程命令执行漏洞(CVE-2016-4977)
影响版本: 2.0.0-2.0.9 1.0.0-1.0.5 poc地址 https://github.com/vulhub/vulhub/blob/master/spring/CVE-2016-497 ...
- SQL 查询总是先执行SELECT语句吗?你们都错了!
SELECT语句中子句的顺序.SELECT语句中使用时必须遵循的次序. 经过一段时间的学习,我们知道了SELECT语句超简版的语法如下: SELECT 字段名 FROM 表名 后来,我们又陆续学习了W ...