void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Downloader.RunThread(worker);                  }
当用户取消时,如何中止这个下载线程啊?

解决方案 »

  1.   

    请求取消backgroundWorker控件挂机的操作就可以了。
    bw.CancelAsync();
      

  2.   

    最好判断一下:            if (backgroundWorker1.IsBusy)
                {
                    backgroundWorker1.CancelAsync();
                }
      

  3.   

    void bw_DoWork(object sender, DoWorkEventArgs e)
            {
              if (worker.CancellationPending)
                {
                    e.Cancel = true;
                }
                else
                {
                         this.Downloader.RunThread(worker);         
                }
            } 
      

  4.   

    点取消按钮的事件执行  bgWorker.CancelAsync();
      

  5.   

    你这个也不行啊。dowork一直检测这个CancellationPending吗,即使为真,也只是把e.Cancel = true,一旦runthread运行,也没把他停止啊
      

  6.   

    在bw_DoWork里执行的代码本来就在工作线程里,你不需要再启动一个线程,把线程里面的代码移出来。
      

  7.   

    当用户取消时候,得写个事件,让这个事件去判断,bg是否busy,是就cancel掉。
      

  8.   

    你的程序代码是否放在worker的另一个线程中运行了
    this.Downloader.RunThread(worker);       参考下backgroundworker的用法,需要把运行的代码放在dowork中的,而不是另外开个线程。   
      

  9.   

    //纯手写的,没有在ide中测试过,仅供参考private Thread currentThread=null;
    void DoWork(object sender, DoWorkEventArgs e)
    {
       //获取工作者的线程
       currentThread=Thread.CurrentThread;
       //DoSomeWork();
    }
    //调用这个方法来立即终止work
    public void Cancel()
    {
    if(currentThread!=null)
    {
    currentThread.Abort();
    currentThread=null;
    }
    }