Calling ShowDialog in BackgroundWorker

后端 未结 4 890
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 17:46

I have a WinForms application in which my background worker is doing a sync task, adding new files, removing old ones etc.

In my background worker code I want to sho

4条回答
  •  独厮守ぢ
    2020-12-11 18:16

    I usually create a method to execute a delegate on the UI thread:

      private void DoOnUIThread(MethodInvoker d) {
         if (this.InvokeRequired) { this.Invoke(d); } else { d(); }
      }
    

    With this, you can change your code to:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
       DialogResult result = DialogResult.No;
       DoOnUIThread(delegate() {
          MyForm f = new MyForm();
          f.FilesToAddDelete(..);
          result = f.ShowDialog();
       });
    
       if(No...)
       return;
       else
       //keep working...
    }
    

提交回复
热议问题