• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风拂晚柳
博客园    首页    新随笔    联系   管理    订阅  订阅

C#-----CancellationTokenSource取消线程

   1. 使用CancellationTokenSource退出Thread

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TokenSourceTest
{
    public partial class Form1 : Form
    {
        private CancellationTokenSource cts = null;
        private int i = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cts = new CancellationTokenSource();
            this.listBox1.Items.Clear();
            Thread thread = new Thread(new ThreadStart(ThreadTest));
            thread.IsBackground = true;
            thread.Start();
            this.button1.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cts.Cancel();
            this.labelThread.Text="线程一已取消";
            this.button1.Enabled = true;
        }

        public void ThreadTest()
        {
            while (!cts.IsCancellationRequested)
            {
                this.listBox1.Invoke((MethodInvoker)delegate ()
                {
                    this.listBox1.Items.Add(i++);
                });
                Thread.Sleep(200);
            }
        }
    }
}

   2. 使用CancellationTokenSource退出Task

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TokenSourceTest
{
    public partial class Form1 : Form
    {
        private CancellationTokenSource cts = null;
        private int i = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            cts = new CancellationTokenSource();
            Task task = new Task(() =>
            {
                //获取是否已请求取消此线程
                while (!cts.IsCancellationRequested)
                {
                    //Invoke在拥有此控件的基础窗口句柄的线程上执行指定的委托
                    this.listBox1.Invoke((MethodInvoker)delegate ()
                    {
                        this.listBox1.Items.Add(i++);
                    });
                    Thread.Sleep(200);
                }
            });
            task.Start();
            this.button4.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cts.Cancel();
            this.labelThread.Text = "线程二取消";
            this.button4.Enabled = true;
        }
    }
}

 

posted @ 2019-09-16 10:35  风拂晚柳  阅读(590)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3