注意点:需要将每个线程的 MemoryCacheManager 保存,这里我保存在缓存中,需要取消时根据缓存key值取出 MemoryCacheManager

        //开始Task1
private void button1_Click(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;//winform运行跨线程 ICacheManager cacheManager = new MemoryCacheManager();
var cts1 = new CancellationTokenSource();
CancellationToken ct1 = cts1.Token;
Task.Factory.StartNew(() =>
{
while (true)
{
Thread.Sleep();
listView1.Items.Add("AA当前时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (ct1.IsCancellationRequested)
{
// another thread decided to cancel
listView1.Items.Add("task1 canceled");
cacheManager.Remove("cts1");
break;
}
}
}, ct1); if (!cacheManager.IsSet("cts1"))
{
cacheManager.Set("cts1", cts1, );
}
} //开始Task2
private void button2_Click(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;//winform运行跨线程 ICacheManager cacheManager = new MemoryCacheManager();
var cts2 = new CancellationTokenSource();
CancellationToken ct2 = cts2.Token;
Task.Factory.StartNew(() =>
{
while (true)
{
Thread.Sleep();
listView1.Items.Add("BB当前时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (ct2.IsCancellationRequested)
{
// another thread decided to cancel
listView1.Items.Add("task1 canceled");
cacheManager.Remove("cts2");
break;
}
}
}, ct2); if (!cacheManager.IsSet("cts2"))
{
cacheManager.Set("cts2", cts2, );
}
} //停止Task1
private void button3_Click(object sender, EventArgs e)
{
ICacheManager cacheManager = new MemoryCacheManager();
var cts = cacheManager.Get<CancellationTokenSource>("cts1");
cts.Cancel();
} //停止Task2
private void button4_Click(object sender, EventArgs e)
{
ICacheManager cacheManager = new MemoryCacheManager();
var cts = cacheManager.Get<CancellationTokenSource>("cts2");
cts.Cancel();
}

Task 开始 停止的更多相关文章

  1. gulp-less解决遇到错误停止执行task

    来龙去脉 在用less+gulp开发时,有时候代码还没写完整,不小心保存了一下,然后gulp就开始执行gulp-less的task. 但是代码是有问题的,这时候会输出一个Potentially unh ...

  2. Android中关于Task的一些认识

    Android中Task是一个逻辑上的概念,简单地说,就是一个栈里面顺序存储着的多个Activity.这些Activity能够是来自同一个App,也能够是来自不同的Apps. Task的创建 比方之前 ...

  3. JavaScript-setInterval-周期性行定时器-倒计时

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. Linux Kernel 排程機制介紹

    http://loda.hala01.com/2011/12/linux-kernel-%E6%8E%92%E7%A8%8B%E6%A9%9F%E5%88%B6%E4%BB%8B%E7%B4%B9/ ...

  5. 常用Vxworks编程API

    vxWorks编程API 一.官方的Program Guide 位于安装目录下:\docs\vxworks\guide\index.html 二.常用的库: #i nclude "taskL ...

  6. client多线程

    1.多线程对象 对象可以是多线程访问,线程可以在这里分为两类: 为完成内部业务逻辑的创建Thread对象,线程需要访问对象. 使用对象的线程外部对象. 进一步假设更精细的划分.业主外螺纹成线等线,. ...

  7. C# QuartZ使用实例写成服务

    官方学习文档:http://www.quartz-scheduler.net/documentation/index.html 官方的源代码下载:http://sourceforge.net/proj ...

  8. 2017-2018-20172311 暑期编程作业:APP

    2017-2018-20172311 暑期编程作业:实现一个简单倒计时APP 写在前面:暑假的时候就单纯的想要设计一个倒计时软件,然后就通过查阅资料等学了一些,包括实现倒计时功能及显示:背景音乐的添加 ...

  9. C++11线程池的实现

    什么是线程池 处理大量并发任务,一个请求一个线程来处理请求任务,大量的线程创建和销毁将过多的消耗系统资源,还增加了线程上下文切换开销. 线程池通过在系统中预先创建一定数量的线程,当任务请求到来时从线程 ...

随机推荐

  1. Oracle 查询表分区相关信息

    Oracle 查询表分区相关信息 --表分区 --1,分区表信息 -- (1)显示数据库所有分区表的信息 select * from DBA_PART_TABLES a where a.owner=u ...

  2. 003 spring boot访问静态资源与重定向

    今天被问到重定向的问题,后续又引起了静态资源路径配置的问题,在这里做一个总结,当然,顺便添加默认访问index.html. 一:默认访问 1.默认路径 在springboot中静态资源的映射文件是在r ...

  3. hyper-v启动虚拟机时提示“The application encountered an error while attempting to change the state of the machine ‘虚拟机名称'”如何处理?

    1. 找出发生这一问题的事件代号 1.1 在开始菜单中搜索程序Event Viewer并点击进入 1.2 点击路径如下: “Applications and Services Logs > Mi ...

  4. C++main函数命令行选项——学习笔记

    atoi字符串的数转化为整数 atof转化为小数

  5. Web 性能压力测试工具之 Siege 详解

    Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行.s ...

  6. flutter Slider滑块组件

    滑块,允许用户通过滑动滑块来从一系列值中选择. import 'package:flutter/material.dart'; class SliderDemo extends StatefulWid ...

  7. Flutter 页面布局 Stack层叠组件

    Stack 表示堆的意思,我们可以用 Stack 或者 Stack 结合 Align 或者 Stack 结合 Positiond 来实现页面的定位布局 属性 说明 alignment 配置所有子元素的 ...

  8. JQuery中formSerialize()报错:对象不支持"formSerialize"属性或方法

    formSerialize()是jQuery的表单插件中提供的方法. formSerialize()的核心方法是:$.param(data); Form表单经过formSerialize(),被序列化 ...

  9. SQL Server判断对象是否存在

    1 判断数据库是否存在 if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名] if exis ...

  10. unsupported media type 415

    jquery  ajax请求报错解决方案: 两点: data: JSON.stringify(obj), contentType:"application/json", 完整pos ...