简体   繁体   English

C#Backgroundworker关闭表格

[英]C# Backgroundworker closing form

I am having an issue with a background worker. 我与背景工作者有问题。 When i'm cancelling the backgroundworker, it closes the form too, i don't want it. 当我取消backgroundworker时,它也关闭了表单,我不想要它。 I just want to stop the backgroundworker, and keep the form on the screen, and saying a message to the user like "Program stopped by user" 我只想停止backgroundworker,并将表格保留在屏幕上,然后向用户说“程序被用户停止”这样的消息

public BackgroundWorker bw = new BackgroundWorker() { WorkerSupportsCancellation = true };
public Form1()
        {
            bw.WorkerSupportsCancellation = true;
            InitializeComponent();
        }

private void stopChild_Click(object sender, EventArgs e)
        {
            if (bw.IsBusy)
            {
                bw.CancelAsync();
                this.Dispose();
            }
        }

public void startParListe()
        {
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            if (bw.IsBusy != true)
                bw.RunWorkerAsync();
        }

public void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker ;
    for (int i = 0; i < countPlages; i++)
    {
         if ((worker.CancellationPending == true)) 
         {
             e.Cancel = true;
         }
         #do something else
    }
}

So, when the user click on the " stopChild " button, it is supposed to send a CancelSignal to my BWorker. 因此,当用户单击“ stopChild ”按钮时,应该向我的BWorker发送一个CancelSignal。 Everytime my BWorker is working, i'm checking for this CancellationPending Signal before doing anything. 每当我的BWorker工作时,我都会在做任何事情之前检查这个CancellationPending信号。

With this code, the form closes when clicking on the " stopChild " And when i remove the "this.Dispose()" , the BWorker doesn't stop. 使用此代码,单击“ stopChild ”时将关闭该窗体,并且当我删除“ this.Dispose()”时 ,BWorker不会停止。 Why ? 为什么呢

You don't properly process the CancellationPending : 您没有正确处理CancellationPending

     if ((worker.CancellationPending == true)) 
     {
         e.Cancel = true;
     }

I am not sure what e.Cancel is supposed to do, but you still continue to do work in your for loop since you don't leave the loop. 我不确定e.Cancel应该做什么,但是您仍然继续在for循环中进行工作,因为您没有离开循环。 Change it to: 更改为:

     if (worker.CancellationPending) // btw - avoid unnecessarily verbous code
     {
         break;
     }

The backgroundworker does not close your form, this.Dispose does. 后台工作人员不会关闭您的表单,而this.Dispose会关闭。 You need to omit it. 您需要忽略它。 The reason why this.Dispose helped stop the background worker is because it is owned by the form, so closing the form also closees the worker. this.Dispose帮助阻止后台工作人员的原因是因为它由表单拥有,所以关闭表单也将关闭工作人员。 But once you fix the cancellation as described above, you should not need Dispose any longer 但是,一旦您如上所述解决了取消问题,就不再需要“ Dispose

You should break out of the loop when you set e.Cancel to true (also, this.Dispose() is closing your form: 将e.Cancel设置为true时,您应该跳出循环(此外,this.Dispose()正在关闭表单:

public void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker ;
    for (int i = 0; i < countPlages; i++)
    {
         if ((worker.CancellationPending == true)) 
         {
             e.Cancel = true;
             break;
         }
         #do something else
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM