时间:2019-03-17 标签:c#backgroundWorker不引发ProgressChanged或RunWorkerCompleted事件
使用 C# .NET 3.5。
似乎我不是唯一的人,因为搜索似乎提出了很多类似的问题,但是通读它们我似乎仍然缺少解决方案。
我通过单击 UI 上的按钮来引发一个事件,然后该事件应该启动一个 backgroundWorker 来完成一些耗时的工作 - 在这种情况下,我希望它从表单收集信息并 a) 写入 xml文件以及 b) 将信息插入远程数据库。
我相信这正是backgroundWorker 的设计和意图。
这是事件引发代码。
private void btnFinish_Click(object sender, EventArgs e)
{
clkFinish.setInstant();
curAct.setTimeFinish(DateTime.Now);
btnStart.Enabled = true;
bgwDataWorker.RunWorkerAsync();
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Thread a: " + i);
Thread.Sleep(100);
if (i == (20 - 1))
{
Console.WriteLine("Finished");
}
}
}
正如您所看到的,我在那里有一些代码,我用它们来平衡后台工作代码,如下所示:
private void bgwDataWorker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine("Running in a different thread now");
int count = 0;
for (int i = 0; i < 21; i++)
{
Console.WriteLine("Thread b: " + i);
Thread.Sleep(100);
(sender as BackgroundWorker).ReportProgress(5 * i, null);
if (i == (21 - 1))
{
count = i;
}
}
e.Result = count;
}
到目前为止,一切似乎都在进行。
我的问题是,当 DoWork 方法中的代码完成时,什么也没有发生。以下是 ProgressChanged 和 RunWorkerComplete 方法。
private void bgwDataWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage);
}
private void bgwDataWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("Result is " + e.Result.ToString());
}
这让我很困惑。
我尝试运行 示例来自 MSDN ,我也遇到了同样的问题。由于某种原因, RunWorkerCompleted 和 ReportProgress 事件根本没有被引发,我不明白为什么。
感谢您的阅读。
艾伦
Using C# .NET 3.5.
It seems I'm not the only one, since a search seems to bring up a lot of similar questions, however reading through them I seem still to be missing the solution.
I'm raising an event with a button click on my UI, which is then supposed to start a backgroundWorker to get some time consuming work done - in this case, I want it to collect information from a form and a) write to an xml file as well as b) insert the information into a remote database.
This is exactly what the backgroundWorker was designed and intended to do I believe.
Here is the event raising code.
private void btnFinish_Click(object sender, EventArgs e)
{
clkFinish.setInstant();
curAct.setTimeFinish(DateTime.Now);
btnStart.Enabled = true;
bgwDataWorker.RunWorkerAsync();
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Thread a: " + i);
Thread.Sleep(100);
if (i == (20 - 1))
{
Console.WriteLine("Finished");
}
}
}
As you can see, I have some code there which I've used as a counterbalance to the background worker code, which is here:
private void bgwDataWorker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine("Running in a different thread now");
int count = 0;
for (int i = 0; i < 21; i++)
{
Console.WriteLine("Thread b: " + i);
Thread.Sleep(100);
(sender as BackgroundWorker).ReportProgress(5 * i, null);
if (i == (21 - 1))
{
count = i;
}
}
e.Result = count;
}
So far, everything seems to be working up to this point.
My problem is that when the code in the DoWork method is finished, nothing is happening. Here are the ProgressChanged and RunWorkerComplete methods.
private void bgwDataWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage);
}
private void bgwDataWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("Result is " + e.Result.ToString());
}
This has me baffled.
I've tried running the examples from MSDN and I'm experiencing the same problems. RunWorkerCompleted and ReportProgress events are simply not being raised for some reason and I don't understand why.
Thanks for reading.
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我有同样的问题,我刚刚找到了答案。
在后台工作程序的属性中,确保 ProgressChanged 和 RunWorkerCompleted 列出其函数的名称(我的函数分别称为“backgroundWorker1_ProgressChanged”和“backgroundWorker1_RunWorkerCompleted”)。
I had the same problem and I just figured out the answer.
In the properties of the backgroundworker, make sure that ProgressChanged and RunWorkerCompleted list the name of their functions (mine are called 'backgroundWorker1_ProgressChanged' and 'backgroundWorker1_RunWorkerCompleted' respectively).
您没有错过设置这些变量,是吗?
它也可能是 DoWork-Method 中的异常,未处理。
You didn´t miss to set these variables, did you?
Also it could be an exception in the DoWork-Method, that isn´t handled.
Inksmithy,您提到您已重命名BackgroundWorker。请注意,当您执行此操作时,表单设计器不会自动更改对旧 DoWork 和 RunWorkerCompleted 方法的引用。因此,在表单设计器中,单击“BackgroundWorker”,然后单击“属性”窗口中的“事件”图标(闪电),并仔细检查它是否指向正确的事件方法名称。
Inksmithy, you mentioned you have renamed the BackgroundWorker. Please note that when you do this, the Forms Designer does NOT automatically change the reference to the old DoWork and RunWorkerCompleted methods. So in your Forms designer, click on your BackgroundWorker, then click on the Events icon (lightning bolt) in the Properties Window, and double-check that it is pointing to the correct Event Method Names.
将 WorkerReportsProgress 属性设置为 True。
Set
WorkerReportsProgress
property to True.你遗漏了一个重要的部分 - 一个布尔标志,允许它引发事件
You've left out a vital bit - a boolean flag to allow it to raise the events
我有一个项目 [A](不久前构建),进度条可以正常工作。当前应用程序 [B] 未报告进度。由于某种原因,应用程序 [A] 连接了进度和工作完成事件处理程序。因此,我在应用程序 [B] 中连接了事件处理程序,并且它正在工作。处理程序被复制到 Designer.cs 部分类文件中。 (假设它们可以进入 form.cs 部分类)。
希望这有帮助。不知道为什么应用程序[A]是通过使用工具箱将后台工作人员放置在表单设计窗口上来连接的。使用 VS2012 和 Windows 7。
另外,我正在计算进度百分比,由于某种原因,列表框中的项目数乘以所选复选框的数量超过了 100% - 它抛出了错误。因此,我将百分比限制为 99,并将其作为向事件报告的进度百分比的逻辑的一部分。
I had one project [A], (built a while back) that had the progress bar working. The current app [B] is not reporting progress. For some reason app [A] had the progress and work completed event handlers wired up. So, I hooked up the event handlers in app [B] and it is working. The handlers are copied into the designer.cs partial class file. (Assuming they could go in the form.cs partial class).
hope this helps. Don't know why the app [A] was wired up by using the toolbox to drop the background worker on the form design window. Using VS2012 and Windows 7.
Also, I am calculating the progress percentage and for some reason the number of items in the listbox times the number of selected checkboxes exceeded 100% - it threw and error. So, I will cap the percentage at 99 and make that part of my logic for the progress percentage being reported back to the event.
我最近遇到了这个问题。这是因为我让主线程休眠直到后台线程完成,这阻止了更新的执行。
我通过将我的对象(包含工作线程)包装到另一个线程中解决了这个问题,以便可以执行进度更新,更改主线程中的值,从而允许我的休眠线程继续。
I had this issue recently. It was caused because I had the main thread sleeping until the background thread was finished, which blocked the update from executing.
I solved this by wrapping my object (containing worker threads) into another thread so the progress update could execute, change the values in the main thread allowing my sleeping thread to continue.