在BackgroundWorker中的Winform上更新状态 [英] Updating a status on a Winform in BackgroundWorker
问题描述
我有一个多步骤的BackgroundWorker流程.我使用选取框进度栏,因为这些步骤中的几个步骤是在iSeries服务器上运行的,因此没有确定百分比的好方法.我设想的是在每个步骤之后都带有更新的标签.您如何建议在Winform上更新标签以反映每个步骤?
I have a multi-step BackgroundWorker process. I use a marquee progress bar because several of these steps are run on a iSeries server so there isn't any good way to determine a percentage. What I am envisioning is a label with updates after every step. How would you recommend updating a label on a winform to reflect each step?
我想补充一点.我通过iSeries(或IBM i或AS/400或运行OS/400 ... er ... i5/OS的中型计算机)上的存储过程调用了一些CL和RPG程序.年复一年)).
Figured I would add a bit more. I call some CL and RPG programs via a stored procedure on an iSeries (or IBM i or AS/400 or a midrange computer running OS/400... er... i5/OS (damn you IBM for not keeping the same name year-to-year)).
无论如何,我必须等到该步骤完全完成后,才能继续在Winform端进行操作.我当时正在考虑将反馈信息发送给用户,并提供主要步骤.
Anyway I have to wait until that step is fully complete before I can continue on the winform side. I was thinking of sending feedback to the user giving the major steps.
- 将数据转储到iSeries
- 运行月底
- 创建报告
- 上传最终结果
我可能一开始就应该给出这个.对于那个很抱歉.我会尽量保持问题的一般性,以供其他人以后使用,而不是我的特定任务.
I probably should have given this in the beginning. Sorry about that. I try to keep my questions general enough for others to make use of later rather than my specific task.
推荐答案
从本质上讲,这是后台工作人员的要点之一.根据您的算法,使用ProgressBar
并仅确定进度.
This is one of the points of a background worker in essence. Use a ProgressBar
and just determine how far along the progress is, according to your algorithm.
(如前所述,如果通过率达到10%,则发送10
;如果通过率达到50%,则发送50
)
(As has been mentioned, if they're 10% through, send 10
, if they're 50% through, send 50
)
使用BackgroundWorker
bgWrk
Using a BackgroundWorker
bgWrk
添加以下事件:
bgWrk.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(bgWrk_ProgressChanged);
// Note: This method is invoked on the UI thread
void bgWrk_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
// Add progress to whatever UI element needs updating. The below simply uses a progress bar.
prog.Value = e.ProgressPercentage;
}
在您认为值得用户更新的每个主要步骤之后,请执行以下操作:
After each major step that you think deserves a user updates do the following:
bgWrk.ReportProgress(intValue);
一些注意事项:
-
您也可以在
ReportProgress()
方法中传递Object
,因此您可以使用字符串对象等更新标签,但是进度条仍然是保持"的通用符号,我在做什么"
You can pass an
Object
as well in theReportProgress()
method, so you would be able to update a label with a string object etc, however a progress bar is still the universal symbol of "hold on, i'm doing something"
如果有不确定的轮询,并且使用的是ProgressBar
,请尝试将其用作不确定的ProgressBar或微调器等. WPF具有内置属性,可以使进度条不确定,这很有用.
If you have any indeterminate polling, and you are using a ProgressBar
, try use it as an Indeterminate ProgressBar, or a spinner or such. WPF has a built in property to make a progress bar indeterminate which is useful.
这篇关于在BackgroundWorker中的Winform上更新状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!