【问题标题】:BackgroundWorker Event Never ExecutesBackgroundWorker 事件从不执行
【发布时间】:2017-12-13 00:57:10
【问题描述】:

我正在尝试使用Backgroundworker 来保持我的主 UI 线程打开而不是冻结。我正在单步执行我的代码,并在backgroundWorker1.RunWorkerAsync(); 上设置了一个断点,它一旦命中就离开了方法,并且在foreach 行上设置了一个断点 -> 永远不会被命中。

使用 Backgroundworker 的正确方法是什么?

public Form1()
{
    InitializeComponent();
    backgroundWorker1.WorkerReportsProgress = true; 
    backgroundWorker1.WorkerSupportsCancellation = true;
}
private void btnQuery_Click(object sender, EventArgs e)
{
    grid1.Rows.Clear();
    backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (string name in studentRoster)
    {
        InsertIntoDB();
    }
}

【问题讨论】:

  • 检查您的后台工作人员是否忙。如果它已经在运行,它将不会再次运行。 if (!backgroundWorker1.IsBusy) { backgroundWorker1.RunWorkerAsync(); }
  • 不忙。我用你的语法验证了:)
  • 确保backgroundWorker1 += backgroundWorker1_DoWork 存在。也许它在 Form1.designer.cs 中?

标签: c# asynchronous backgroundworker


【解决方案1】:

这是您添加的处理程序和一些 cmets 的代码。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);

    }
    private void btnQuery_Click(object sender, EventArgs e)
    {
        grid1.Rows.Clear();
        backgroundWorker1.RunWorkerAsync();
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        foreach (string name in studentRoster)
        {
            InsertIntoDB();

            // You can report progress by calling the following function.
            //backgroundWorker1.ReportProgress(int percentProgress, object userState)
            // You can set the percentProgress to any valid integer value,
            // and userState can be any object you want.

            // You can also check to see if this operation has been sent a request to cancel.
            if (backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
        }

        // You can send information back to the main thread by setting e.Result to any object you want.
    }
    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Do something with the event that is being raised.
        // To pass a value back through to this event, use the percentProgress and userState
        // parameters of the ReportProgress function.
        // the userState object that you pass will be received here as e.UserState
    }
    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // This event is raised by the background worker when the DoWork method is completed.
        // You can receive information back from the worker thread by evaluating e.Result
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多