如何使用的BackgroundWorker在C# [英] How to use BackgroundWorker in C#

查看:110
本文介绍了如何使用的BackgroundWorker在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用的BackgroundWorker在C#?



其实我执行从调用的方法填写填写的PDF表单的操作() 。它需要更多的时间来显示结果为PDF查看器,所以我决定显示了处理图像使用BackgroundWorker的,并使用它,但没能实现它。



<试图p >这里是我的代码片段:

 私人无效bgwLoadFile_DoWork(对象发件人,DoWorkEventArgs E)
{
this.Invoke((MethodInvoker)委托()
{
????
});
}

私人无效bgwLoadFile_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
如果(e.Cancelled ==真)
{
}
,否则如果(e.Error!= NULL)
{
}
,否则
{
picLoading.SendToBack();
}
}



填写时按钮的填充方法被调用是被点击

 私人无效btnFill_Click(对象发件人,EventArgs五)
{
如果(btnFill.Text ==填充)
{
bgwLoadFile.RunWorkerAsync();
picloading.BringToFront();

填充();
}



笏的说法应该我需要在DoWork的方法添加,如果我试图添加FILL()填充是被称为两次...



任何一个可以帮助我



感谢


解决方案

因此,让我们试图找到一些答案:



该方法 worker_DoWork()将被另一个线程中执行。通过该方法 this.Invoke()内调用你要通过呼叫切换到GUI线程,这使得后台工作的使用没用。取而代之的辅助方法中,你必须调用需要一些时间的方法和不使用GUI 交互。如果这个所谓的方法产生的任何结果(例如有返回值),你应该将此信息写入到变量 e.Result



该方法 worker_RunWorkerCompleted()将在GUI线程内再次调用。让您拍摄效果,让它以某种方式与GUI交互。由于这样的事实,该方法将在GUI中执行的线程应该在其这样做,否则你的GUI将要再次冻结很简单(或快速)。



因此,考虑这些信息可以清理你的代码:

 私人无效btnFill_Click(对象发件人,EventArgs五)
{
如果(btnFill.Text ==填充)
{
//更新为用户
// GUI和开始我们的长期运行的任务
// (禁用按钮等,使
//用户仍然可以点击它们!)。
picloading.BringToFront();
bgwLoadFile.RunWorkerAsync();
}
}

私人无效bgwLoadFile_DoWork(对象发件人,DoWorkEventArgs E)
{
//我们调用长期运行的任务
/ /,并等待它的完成。
填充();
}

私人无效bgwLoadFile_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
//我们回到GUI线程。
//因此,让我们展示某些结果给用户。
如果(e.Cancelled)
{
//为了支持取消长时间运行
//方法来检查某种取消
//标志(布尔字段),允许它快速退出。
labelMessage.Text =操作被取消。
}
,否则如果(e.Error!= NULL)
{
labelMessage.Text = e.Error.Message;
}

//隐藏图像,以允许用户
//再次访问GUI。
//(重新启用再次按钮等)
picLoading.SendToBack();
}


How to use BackgroundWorker in C#?

Actually i'm performing an operation of filling a PDF-Form from method called fill(). It takes more time to show up the result into pdfviewer, so I decided to show up a 'processing image' using a backgroundworker, and tried using it but failing to achieve it

here is my code snippet :

        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 method is called when button FILL is been clicked

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

                Fill();
            }

wat statement should i need to add in DoWork method , if i tried to add FILL() fill is been called twice ...

can any one help me out

Thanks

解决方案

So let's try to find some answers:

The method worker_DoWork() will be executed within another thread. By calling within that method this.Invoke() you're going to pass the call back to the gui thread, which makes the usage of the background worker useless. Instead within the worker method you have to call the method that needs some time and doesn't interact with the gui. If this called method produces any result (e.g. has a return value) you should write this information into the variable e.Result.

The method worker_RunWorkerCompleted() will be called within the gui thread again. Allowing you to take the result and let it somehow interact with the gui. Due to the fact, that this method will be executed on the gui thread it should be quite simple (or fast) in its doing otherwise your gui is going to freeze again.

So given these informations lets clean up your code:

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();
}

这篇关于如何使用的BackgroundWorker在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆