【问题标题】:How to use BackgroundWorker in C#如何在 C# 中使用 BackgroundWorker
【发布时间】:2013-03-01 07:40:21
【问题描述】:

如何在 C# 中使用 BackgroundWorker?

实际上,我正在执行从名为fill() 的方法填写 PDF 表单的操作。将结果显示到 pdfviewer 需要更多时间,所以我决定使用 backgroundworker 显示“处理图像”,并尝试使用它但未能实现它

这是我的代码 sn-p:

        private void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Invoke((MethodInvoker)delegate()
           {
               ???? 
           }); 
        }

        private void bgwLoadFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
          if (e.Cancelled == true)
          {
          }
          else if (e.Error != null)
          {
          }
          else
          {
           picLoading.SendToBack();
          }
       }

当点击FILL按钮时调用Fill方法

private void btnFill_Click(object sender, EventArgs e)
        {
            if (btnFill.Text == "Fill")
            { 
                bgwLoadFile.RunWorkerAsync();
                picloading.BringToFront();

                Fill();
            }

我是否需要在 DoWork 方法中添加 wat 语句,如果我尝试添加 FILL() 填充会被调用两次 ...

谁能帮帮我

谢谢

【问题讨论】:

    标签: c# backgroundworker


    【解决方案1】:

    Fill(); 添加到您的bgwLoadFile_DoWork 并将其从btnFill_Click 中删除

    只是一个旁注,你可能想在那个“其他”之外调用你的picLoading.SendToBack();,就好像你出错或取消它会留在那里一样。

    【讨论】:

      【解决方案2】:

      所以让我们试着找到一些答案:

      worker_DoWork() 方法将在另一个线程中执行。通过在该方法this.Invoke() 中调用,您将把调用传递回 gui 线程,这使得后台工作线程的使用变得无用。相反,在工作方法中,您必须调用需要一些时间并且不与 gui 交互的方法。如果这个被调用的方法产生任何结果(例如有一个返回值)你应该把这个信息写入变量e.Result

      worker_RunWorkerCompleted() 方法将在 gui 线程中再次被调用。允许您获取结果并让它以某种方式与 gui 交互。由于这个方法将在 gui 线程上执行,所以它应该非常简单(或快速),否则你的 gui 将再次冻结。

      因此,鉴于这些信息,让我们清理您的代码:

      private void btnFill_Click(object sender, EventArgs e)
      {
          if (btnFill.Text == "Fill")
          { 
              // Update the gui for the user
              // and start our long running task
              // (disable buttons etc, cause the
              // user is still able to click them!).
              picloading.BringToFront();
              bgwLoadFile.RunWorkerAsync();
          }
      }
      
      private void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e)
      {
          // Let's call the long running task
          // and wait for it's finish.
          Fill();
      }
      
      private void bgwLoadFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          // We're back in gui thread.
          // So let us show some results to the user.
          if (e.Cancelled)
          {
              // To support cancellation, the long running
              // method has to check some kind of cancel
              // flag (boolean field) to allow fast exit of it.
              labelMessage.Text = "Operation was cancelled.";
          }
          else if (e.Error != null)
          {
              labelMessage.Text = e.Error.Message;
          }
      
          // Hide the picture to allow the user
          // to access the gui again.
          // (re-enable buttons again, etc.)
          picLoading.SendToBack();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多