namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
// create the cancellation token source
private CancellationTokenSource TokenSource = new CancellationTokenSource();
private Task CurrentTask;
public MainWindow()
{
InitializeComponent();
} private void btnStart_Click(object sender, RoutedEventArgs e)
{
// create the cancellation token
CancellationToken token = TokenSource.Token;
// create the task CurrentTask = new Task(() =>
{
for (int i = 0; i < int.MaxValue; i++)
{
if (token.IsCancellationRequested)
{
Debug.WriteLine("Task cancel detected");
break;
//throw new OperationCanceledException(token);
}
else
{
Debug.WriteLine("Int value {0}", i);
}
}
}, token); // start the task
CurrentTask.Start();
} private void btnStop_Click(object sender, RoutedEventArgs e)
{
if (CurrentTask != null)
{
if (CurrentTask.Status == TaskStatus.Running) { }
{
TokenSource.Cancel();
Debug.WriteLine("Task cancel");
}
}
}
}
}

  

TASK的开始与暂停的更多相关文章

  1. Spring 3整合Quartz 2实现手动设置定时任务:新增,修改,删除,暂停和恢复(附带源码)

    摘要:在项目的管理功能中,对定时任务的管理有时会很常见.但一般定时任务配置都在xml中完成,包括cronExpression表达式,十分的方便.但是如果我的任务信息是保存在数据库的,想要动态的初始化, ...

  2. iOS - NSURLSession 网络请求

    前言 NS_CLASS_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0) @interface NSURLSession : NSObject @available(iOS ...

  3. c#异步学习笔记

    如下代码.只需要异步的处理一个数据,不需要等处理完成后,进行后继的操作.可以不用Async来修饰方法 static void Main(string[] args) { Test(); Console ...

  4. ios 后台下载,断点续传总结

    2018年12月05日 16:09:00 weixin_34101784 阅读数:5 https://blog.csdn.net/weixin_34101784/article/details/875 ...

  5. asyncio并发编程

    一. 事件循环 1.注: 实现搭配:事件循环+回调(驱动生成器[协程])+epoll(IO多路复用),asyncio是Python用于解决异步编程的一整套解决方案: 基于asynico:tornado ...

  6. NSURLSession 网络请求

    1.NSURLSession 在 iOS9.0 之后,以前使用的 NSURLConnection 过期,苹果推荐使用 NSURLSession 来替换 NSURLConnection 完成网路请求相关 ...

  7. 12 (OC)* AFNetworking

    AFNetworking主要是对NSURLSession和NSURLConnection(iOS9.0废弃)的封装,其中主要有以下类:1). AFHTTPRequestOperationManager ...

  8. Task 暂停与继续

    static void Main(string[] args) { CancellationTokenSource tokenSource = new CancellationTokenSource( ...

  9. C# Task 暂停与取消 或 C#中可取消的Task

    (1)https://www.cnblogs.com/zhengzc/p/10724839.html (2)https://blog.csdn.net/hxfhq1314/article/detail ...

随机推荐

  1. github(工蜂)密码过期时sourcetree重新登录

  2. P4016 负载平衡问题(最小费用最大流)

    P4016 负载平衡问题 题目描述 GG 公司有 nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nn 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬 ...

  3. wampserver apache 500 Internal Server Error解决办法

    Internal Server ErrorThe server encountered an internal error or misconfiguration and was unable to ...

  4. 3-cmd命令

    1.查看IPC$是否启用 命令:net share 2.启动/停止windows服务 命令:net start MSDTC     net stop MSDTC 3.修改服务的启动类型(start=  ...

  5. js文件中使用el表达式问题

    作者:Sang 单独js文件不能用el表达式. 首先,JSP是由服务端执行的,EL表达式自然也由服务端解析执行,因此如果EL所在的脚本在JSP页面内,它是可以获取到值的,这个值在服务器端返回到浏览器端 ...

  6. 包 ,模块(time、datetime、random、hashlib、typing、requests、re)

    目录 1. 包 1. 优先掌握 2. 了解 3. datetime模块 1. 优先掌握 4. random模块 1. 优先掌握 2. 了解 5. hashlib模块和hmac模块 6. typing模 ...

  7. [暂停维护]基于8211lib库对s57电子海图的解析和存储

    此篇博文停止维护,欢迎移步最新地址(含源代码),https://www.yanlongwang.net/USV/ENC-analysis-store.md/, 查看最新文章. 电子海图是为适用航海需要 ...

  8. Codeforces Round #588 (Div. 2) D. Marcin and Training Camp(思维)

    链接: https://codeforces.com/contest/1230/problem/D 题意: Marcin is a coach in his university. There are ...

  9. 【Android-SwipeRefreshLayout控件】下拉刷新

    Android自带API ,V4包下面的下拉刷新控件 android.support.v4.widget.SwipeRefreshLayout SwipeRefreshLayout只能包含一个控件 布 ...

  10. Python基础面试题整理

    基础 Python中lambda是什么意思 Python中的pass是什么意思 作为解释型语言,Python如何运行 什么是Python的单元测试 在Python中unittest是什么 如何将数字转 ...