本文介绍了BackgroundWorker显示进度,但是主线程可能也需要显示进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决-如果您对如何解决此问题感兴趣,请参阅以下注释,因为这是一种两方解决方案,因此在其他情况下都可能有用.

通常,用户会进入我程序的导入数据"窗口,以开始将数据从文件或电子表格导入到我们的数据库中.但是,根据导入的大小,此过程最多可能需要2分钟.

因此,为减少此时间,我决定通过使用BackgroundWorker在程序初始化过程中抢先开始导入过程的可预测方面.
随着数据导入的进行,数据导入类正忙于引发BackgroundWorker.ReportProgress 事件,并在ProgressChangedEventArgs 中传入状态对象.状态对象具有显示ProgressBar 的所有数据以及其他一些状态,例如导入的数字,图标,弹出消息等,但是尚无UI(尚未)显示此进度.

但是,如果用户足够快,他们可以在完成后台导入之前进入导入数据"窗口. BackgroundWorker中仍在进行中.

因此,在加载该窗口时,我将通过处理BackgroundWorker的ProgressChanged 事件来调入"到导入过程的状态.这使用户在过程完成时感到很开心.

顺便说一句:由于这是Windows Forms应用程序,因此我可以告诉您,尝试从BackgroundWorker线程更新UI控件时,我遇到了很多尝试与错误的跨线程错误.我终于放弃了所有这些,决定尝试使用ProgressChanged 事件.

回到令人难过的故事:但是,此时,用户现在可以决定重新导入,或更改某些阻止导入成功的属性,然后像过去那样手动"开始导入在实施此BackgroundWorker 想法之前.

因此,现在程序正在执行另一次数据导入,当然我想使用与BackgroundWorker 相同的类,但是这一次,它将在UI线程中完成.

因此,我的导入类正在提升到BackgroundWorker BackgroundWorker.ReportProgress事件需要由UI线程来处理.

我有一个模糊的想法,可以使用Delegate 向UI线程或BackgroundWorker 线程报告进度,但是我没有太多使用它们的经验.

所以这是我的问题:如果正在运行,如何使用Delegate 将此进度发送到BackgroundWorker ,但是如果在此启动进程,则如何将其发送到UI?

或者,我是否需要放弃BackgroundWorker的想法并教自己如何使用诸如MSDN文章基于事件的异步模式概述"中所述的更复杂的Event-based Asynchronous Pattern 之类的东西?方案


SOLVED - PLEASE SEE COMMENTS BELOW IF YOU ARE INTERESTED IN HOW THIS WORKED OUT, SINCE IT IS A 2-PART SOLUTION, BOTH OF WHICH ARE POTENTIALLY USEFUL TO OTHERS.

Often, users will go to my program''s "import data" window to initiate an import of data from a file or spreadsheet to our database. Depending on the size of the import, though, this can take up to 2 minutes.

Therefore, to reduce this time, I have decided to get a head start on the predictable aspects of the import process during program initialization, by using a BackgroundWorker.

As the data import progresses, the data import classes are busy raising the BackgroundWorker.ReportProgress event, passing in a state object in the ProgressChangedEventArgs . The state object has all the data to show a ProgressBar and some other state like number imported, an icon, popup message, etc., but there is no UI (yet) to display this progress.

But then if the user is quick enough, they can get to the "import data" window before the background import is done. It is still in progress in the BackgroundWorker.

So when that window loads, I "tune in" to the state of the import process, by handling the ProgressChanged event of the BackgroundWorker. This keeps the user amused while the process completes.

By the way: Since this is a Windows Forms application, I can tell you that I have had lots of trial-and-error fun with cross-thread errors trying to update UI controls from the BackgroundWorker thread. I finally gave up on all that and decided to try and use the ProgressChanged event instead.

Back to the sad story: However, at this point, the user may now decide to re-import, or change some properties that were preventing the success of the import, and proceed to initiate the import "manually", as they used to do before I implemented this BackgroundWorker idea.

So now the program is doing another data import, and of course I want to use the same classes that the BackgroundWorker used, but this time, it''s going to be done in the UI thread.

So the BackgroundWorker.ReportProgress event that my import classes were raising to the BackgroundWorker need to be handled instead by the UI thread.

I have this foggy notion that a Delegate could be used to report progress to either the UI thread or the BackgroundWorker thread, but I don''t have a lot of experience with using them.

So here''s my question: How can I use a Delegate to send this progress to the BackgroundWorker if it''s running, but to the UI if the process is initiated there?

Or, do I need a to abandon the BackgroundWorker idea and teach myself how to use something like the more complicated Event-based Asynchronous Pattern as explained in the MSDN article entitled "Event-based Asynchronous Pattern Overview"?

解决方案


这篇关于BackgroundWorker显示进度,但是主线程可能也需要显示进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:02