例子:

        CancellationTokenSource cts ;
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ Task.Run(() => {
try
{ Thread.Sleep(50000);//鸡肋的地方是如果这个地方需要很就才执行玩的话。。。
if (cts.Token.IsCancellationRequested)
{ throw new OperationCanceledException();
} }
catch(OperationCanceledException ex) { MessageBox.Show("Cancled"); } }); } private void Button_Click(object sender, RoutedEventArgs e)
{
cts = new CancellationTokenSource();
cts.Cancel(); }

  

我感觉这个是个鸡肋,有点脱了裤子放屁的感觉。自己设置一个全局标准退出线程是一样的效果。主要是这个做不到立即退出正在执行的线程的效果。CancellationTokenSource.Cancel() 不能对工作线程产生影响,只是设置一个标志。。。。。。所以很鸡肋,不知道我理解得对不对。

有时候我们需要强制终止线程执行。这个CancellationTokenSource就无能为力了。

还需要如下传统方式来:

       private Thread _myThread ;
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ _myThread = new Thread(SomeThreadMethod);
_myThread.Start();
} private void SomeThreadMethod()
{ // do whatever you want
try
{
while (true)
{ Thread.Sleep(1000000);
Debug.Print(""+DateTime.Now );
} }
catch (Exception ex)
{ MessageBox.Show(ex.Message);
} } [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
private void KillTheThread()
{ _myThread.Abort(); } private void Button_Click(object sender, RoutedEventArgs e)
{
KillTheThread(); }

  

C# CancellationTokenSource.Cancel 取消线程很鸡肋?的更多相关文章

  1. C#协作试取消线程

    https://segmentfault.com/q/1010000017109927using System; using System.Collections.Generic; using Sys ...

  2. pthread中取消线程

    取消线程:告诉一个线程关掉自己,取消操作允许线程请求终止其所在进程中的任何其他线程.不希望或不需要对一组相关的线程执行进一步操作时,可以选择执行取消操作.取消线程的一个示例是异步生成取消条件. 对于c ...

  3. Linux学习笔记23——取消线程

    一 相关函数 1 发送终止信号 #include <pthread.h> int pthread_cancel(pthread_t thread); 2 设置取消状态 #include & ...

  4. linux下pthread_cancel无法取消线程的原因【转】

    转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...

  5. linux下pthread_cancel无法取消线程的原因

    一个线程能够调用pthread_cancel终止同一进程中的还有一个线程,可是值得强调的是:同一进程的线程间,pthread_cancel向还有一线程发终止信号.系统并不会立即关闭被取消线程,仅仅有在 ...

  6. C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)

    1.Abort当前线程,后续程序不会执行 class Program { public static Thread thread1; static void Main(string[] args) { ...

  7. CancellationTokenSource 和 CancellationToken 取消线程

    Main 程序[分别调用三个方法] static void Main(string[] args) { using (CancellationTokenSource cts = new Cancell ...

  8. Linux 进程与线程二(等待--分离--取消线程)

    int pthread_join(pthread_t thr,void **thr_return); pthread_join函数用于挂起当前线程,直至th指定的线程终止为止. 如果另一个线程返回值不 ...

  9. 新学了几个python模块,不是很鸡肋。

    先说一个模块分类(基本上所有模块都是小写开头,虽然规范的写法是变量的命名规范,但是,都是这样写的) 1,C编写并镶嵌到python解释器中的内置模块 2,包好的一组模块的包 3.已经被编译好的共享库, ...

随机推荐

  1. NTL 库函数

    NTL是一个高性能,可移植的C ++库,为任意长度的整数提供数据结构和算法; 可用于整数和有限域上的向量,矩阵和多项式; 可用于任意精度浮点运算. NTL为以下方面提供高质量的最先进算法实现: 任意长 ...

  2. java中的compareto方法的详细介绍

    java中的compareto方法的详细介绍 Java Comparator接口实例讲解(抽象方法.常用静态/默认方法) 一.java中的compareto方法 1.返回参与比较的前后两个字符串的as ...

  3. js中 base64 转二进制

    function base64toBlob(base64,type) { // 将base64转为Unicode规则编码 let bstr = atob(base64, type), n = bstr ...

  4. mysql总结1

    修改表名:alter table table_name rename new_table_name; 添加字段:alter table table_name add column_name type_ ...

  5. mybatis 关联查询和嵌套查询的简单示例

    两个表: Customer 顾客表 create table if not exists customer( customer_id int primary key auto_increment, f ...

  6. c# 使用Split分割 换行符

    c# 使用Split分割 换行符,方法如下(其余方法有空再添加): string str = "aa" + "\r\n" + "bb"; s ...

  7. JAVA如何跳出多层循环

    1. break.continue.return 的区别: break默认是跳出最里层的循环,也就是break所在的最近的那层循环 continue是终止本次循环,继续下次循环 return 结束当前 ...

  8. Codeforces Round #325 (Div. 2) A. Alena's Schedule 暴力枚举 字符串

    A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. [POI2011]DYN-Dynamite

    题目链接:Click here Solution: 直接做似乎不太可行,我们考虑二分 我们设\(f[x]\)表示以\(x\)为根的子树中选择了的节点到\(x\)的距离的最小值,初值为\(inf\) \ ...

  10. source和resource的区别

    idea中,有时新导入的工程会出现 类的标识为红色的J,此时为无效,并且该类不能被编译,这是因为该类所在的文件夹java没有被标记为Sources Root,而放置配置文件的resources文件夹没 ...