在C#中断线程

时间:2014-10-05 19:14:38

标签: c# thread-sleep

有没有办法打断backgroundWorker在此程序中运行的system.thread.sleep方法

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

namespace BackGroundWorkerCancel
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //This simulates a synchronous download


           Thread.Sleep(2000);
           MessageBox.Show("Thread.Sleep done");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //Cancel backgroundWorker1 before thread.sleep method completes



        }


    }
}

0 个答案:

没有答案