如何使用BackgroundWorker

本文关键字:BackgroundWorker 何使用 | 更新日期: 2023-09-27 18:00:18

我知道它有3个方法。在我的程序中,我有一个发送消息的方法。它经常很晚,程序有时根本不发送消息来响应按钮按下。有时,它比我预期的晚了5秒,程序冻结了。我想使用BackgroundWorker按预期发送消息,并允许程序始终正常运行。我在按钮处理程序中有发送消息的代码。现在我把这个等价的代码放在哪里?我希望所有这些仍然可以通过按下按钮来处理。

这是合适的处理程序吗?

backgroundWorker1.RunWorkerAsync();

和:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {}

我要把我的代码放在按钮处理程序中吗?在此之前:

carga.progressBar1.Minimum = 0;
carga.progressBar1.Maximum = 100;

Carga是ProgressBar所在的另一种形式。在这种情况下,我如何使用BackgroundWorker?

如何使用BackgroundWorker

您只能从ProgressChangedRunWorkerCompleted事件处理程序更新进度条,因为它们与UI线程同步。

基本思想是Thread.Sleep只是模拟了这里的一些工作。将其替换为真实的路由呼叫。

public Form1()
{
    InitializeComponent();
    backgroundWorker1.DoWork += backgroundWorker1_DoWork;
    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
    backgroundWorker1.WorkerReportsProgress = true;
}
private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(1000);
        backgroundWorker1.ReportProgress(i);
    }
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

我知道这有点过时,但如果另一个初学者正在经历这一过程,我将分享一些涵盖更多基本操作的代码,这里是另一个例子,还包括取消进程并向用户报告进程状态的选项。我将在上面的解决方案中添加Alex Aza给出的代码

public Form1()
{
    InitializeComponent();
    backgroundWorker1.DoWork += backgroundWorker1_DoWork;
    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
    backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;  //Tell the user how the process went
    backgroundWorker1.WorkerReportsProgress = true;
    backgroundWorker1.WorkerSupportsCancellation = true; //Allow for the process to be cancelled
}
//Start Process
private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}
//Cancel Process
private void button2_Click(object sender, EventArgs e)
{
    //Check if background worker is doing anything and send a cancellation if it is
    if (backgroundWorker1.IsBusy)
    {
        backgroundWorker1.CancelAsync();
    }
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(1000);
        backgroundWorker1.ReportProgress(i);
        //Check if there is a request to cancel the process
        if (backgroundWorker1.CancellationPending)
        {
            e.Cancel = true;
            backgroundWorker1.ReportProgress(0);
            return;
        }
    }
    //If the process exits the loop, ensure that progress is set to 100%
    //Remember in the loop we set i < 100 so in theory the process will complete at 99%
    backgroundWorker1.ReportProgress(100);
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
    {
         lblStatus.Text = "Process was cancelled";
    }
    else if (e.Error != null)
    {
         lblStatus.Text = "There was an error running the process. The thread aborted";
    }
    else
    {
       lblStatus.Text = "Process was completed";
    }
}
相关文章:
  • 没有找到相关文章