How to use BackgroundWorker in C#

2020-02-01 17:16发布

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

2条回答
▲ chillily
2楼-- · 2020-02-01 17:33

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();
}
查看更多
等我变得足够好
3楼-- · 2020-02-01 17:41

Add Fill(); to your bgwLoadFile_DoWork and remove it from btnFill_Click

Just a side-note you'll probably want to call your picLoading.SendToBack(); outside of that 'else' as if you error or cancel it will stay there.

查看更多
登录 后发表回答