Home > Backend Development > C#.Net Tutorial > Detailed explanation of the usage of BackgroundWorker in C# (picture)

Detailed explanation of the usage of BackgroundWorker in C# (picture)

黄舟
Release: 2017-04-13 09:58:11
Original
2564 people have browsed it

This article mainly introduces the detailed explanation of the use of C# BackgroundWorker. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

In C# programs, there are often some CPU-intensive operations that take a long time. If such operations are performed directly on the UI thread, the problem of UI unresponsiveness will occur. . The main way to solve this kind of problem is to use multi-threading, start a background thread, and complete the computing operations in this background thread. However, the thread operation of the native interface is somewhat difficult. If you want to further complete the communication between threads, it will be even more difficult.

Fortunately, .NET Class Library provides a class called BackgroundWorker that can solve this type of problem more elegantly. Although the BackgroundWorker class is relatively simple to use, there are still some details that need to be paid attention to. Below we will introduce its main usage through the demo program. We calculate the cumulative sum from 1 to 100 in the demo. For demonstration, each calculation sleeps for 600 milliseconds. The UI of the demo is:

Usage Overview

Build a BackgroundWorker instance on the form, add time-consuming operations to its DoWorkevent handlingfunction, and then call its RunWorkerAsync method. .

private BackgroundWorker _demoBGWorker = new BackgroundWorker();
_demoBGWorker.DoWork += BGWorker_DoWork;
_demoBGWorker.RunWorkerAsync();
private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
{
  //在这里执行耗时的运算。
  int sum = 0;
  for (int i = 0; i <= 100; i++)
  {
    sum += i;
  }
}
Copy after login

Is it a bit too simple? So let's consider the following question:

What if we want to pass parameters to the operation process?

What should we do if we want to display real-time information on the UI during the calculation process?

What if we want to cancel the ongoing operation?

What should we do if an exception occurs during the operation?

Next we will deal with these problems one by one.

Pass parameters to the calculation process

It is not good to directly write 100 into the calculation process. We also plan to allow users to specify the range of the sum! So you need to pass 100 as a parameter to the calculation process. In the overview, we start the calculation process by calling the RunWorkerAsync method. In fact, this method can accept an object type parameter. Through it, we can pass any data to the calculation process:

//别忘了设置滚动条。
this.progressBarSum.Maximum = 100;
_demoBGWorker.RunWorkerAsync(100);
//下面是更新后的 BGWorker_DoWork 方法:
private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
{
  //在这里执行耗时的运算。
  int endNumber = 0;
  if(e.Argument != null)
  {
    endNumber = (int)e.Argument;
  }
  int sum = 0;
  for (int i = 0; i <= endNumber; i++)
  {
    sum += i;
  }
}
Copy after login

BGWorker_DoWork event processing function passes the calculation information we expect through the Argument attribute of parameter e.

Transfer the message to the UI

Since the calculation process is relatively long, while we display the current progress through the progress bar, we also hope to Display the intermediate results of the calculation on the UI in real time. Of course, BackgroundWorker also provides great support for this use case. It allows us to send messages to the UI thread during the calculation process. Let's take a look at the specific method:

_demoBGWorker.WorkerReportsProgress = true;
_demoBGWorker.ProgressChanged += BGWorker_ProgressChanged;
Copy after login

First, set the WorkerReportsProgress property to true, and then add processing for the ProgressChanged event. Method:

private void BGWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
  //修改进度条的显示。
  this.progressBarSum.Value = e.ProgressPercentage;

  //如果有更多的信息需要传递,可以使用 e.UserState 传递一个自定义的类型。
  //这是一个 object 类型的对象,您可以通过它传递任何类型。
  //我们仅把当前 sum 的值通过 e.UserState 传回,并通过显示在窗口上。
  string message = e.UserState.ToString();
  this.labelSum.Text = message;
}
Copy after login

Continue to update BGWorker_DoWork method:

private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
{
  BackgroundWorker bgWorker = sender as BackgroundWorker;
  int endNumber = 0;
  if(e.Argument != null)
  {
    endNumber = (int)e.Argument;
  }

  int sum = 0;
  for (int i = 0; i <= endNumber; i++)
  {
    sum += i;
    
    string message = "Current sum is: " + sum.ToString();
    //ReportProgress 方法把信息传递给 ProcessChanged 事件处理函数。
    //第一个参数类型为 int,表示执行进度。
    //如果有更多的信息需要传递,可以使用 ReportProgress 的第二个参数。
    //这里我们给第二个参数传进去一条消息。
    bgWorker.ReportProgress(i, message);
    Thread.Sleep(600);
  }
}
Copy after login

OK, now you can see the update of the progress bar and execution information.

Cancel operation

Allowing users to cancel the current operation during execution is a basic design, and BackgroundWorker naturally has good support:

_demoBGWorker.WorkerSupportsCancellation = true;
Copy after login

Same as the WorkerReportsProgress attribute, if we want to support cancellation operations we need to set the WorkerSupportsCancellation attribute to true. And we need to support it in the BGWorker_DoWork method. Add code after Thread.Sleep(600) in the for

loop:

 bgWorker.ReportProgress(i, message);
Thread.Sleep(600);

//在操作的过程中需要检查用户是否取消了当前的操作。
if (bgWorker.CancellationPending == true)
{
  e.Cancel = true;
  break;
}
Copy after login

If the cancel

button clicked by the user is detected , just exit the current calculation process. The following is the code to be called when clicking the cancel button:

_demoBGWorker.CancelAsync();
Copy after login

Cancel operation is now supported, try it now!

Exception handling

What to do if an exception occurs during calculation? Is there a way to know that the calculation process has ended? Of course there is, even if it ends normally, we still need to get the calculated results.

_demoBGWorker.RunWorkerCompleted += BGWorker_RunWorkerCompleted;
private void BGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  //如果用户取消了当前操作就关闭窗口。
  if (e.Cancelled)
  {
    this.Close();
  }

  //计算已经结束,需要禁用取消按钮。
  this.btnCancel.Enabled = false;

  //计算过程中的异常会被抓住,在这里可以进行处理。
  if (e.Error != null)
  {
    Type errorType = e.Error.GetType();
    switch (errorType.Name)
    {
      case "ArgumentNullException":
      case "MyException":
        //do something.
        break;
      default:
        //do something.
        break;
    }
  }

  //计算结果信息:e.Result
  //use it do something.
}
Copy after login

RunWorkerCompleted event handler function will be called after the DoWork event handler function returns. Through it, we can perform some operations after the operation, such as disabling the cancel button, exception handling, result display, etc.


Note that if you want to get e.Result, you need to set the e.Result attribute in the BGWorker_DoWork method, such as:

e.Result = sum;
Copy after login
Summary, the BackgroundWorker class is fully functional and easy to use , it is really a powerful tool for handling asynchronous time-consuming operations!

The above is the detailed content of Detailed explanation of the usage of BackgroundWorker in C# (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template