在BackgroundWorker中更新Winform上的状态

发布于 2024-08-25 12:12:00 字数 470 浏览 10 评论 0原文

我有一个多步骤的BackgroundWorker 流程​​。我使用选取框进度条,因为其中几个步骤是在 iSeries 服务器上运行的,因此没有任何好的方法来确定百分比。我设想的是每一步后都会有更新的标签。您建议如何更新 winform 上的标签以反映每个步骤?

我想我会再补充一点。我通过 iSeries(或 IBM i 或 AS/400 或运行 OS/400 的中型计算机...呃...i5/OS(该死的 IBM 不保留相同的名称)上的存储过程调用一些 CL 和 RPG 程序年复一年))。

无论如何,我必须等到该步骤完全完成后才能继续在 winform 方面。我正在考虑向用户发送反馈,给出主要步骤。

  1. 将数据转储到 iSeries
  2. 运行月末
  3. 创建报告
  4. 上传最终结果

我可能应该在一开始就给出这一点。对此感到抱歉。我尽量让我的问题足够笼统,以便其他人稍后使用,而不是我的具体任务。

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?

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)).

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.

  1. Dumping data to iSeries
  2. Running month-end
  3. Creating reports
  4. Uploading final results

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别挽留 2024-09-01 12:12:00

这本质上是后台工作者的要点之一。使用 ProgressBar 并根据您的算法确定进度有多远。

(如前所述,如果通过率为 10%,则发送 10,如果通过率为 50%,则发送 50

使用 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;
}

在您认为值得用户更新的每个主要步骤之后,执行以下操作:

bgWrk.ReportProgress(intValue);

一些注意事项:

  • < p>您也可以在 ReportProgress() 方法中传递 Object ,因此您可以使用字符串对象等更新标签,但是进度条是仍然是“等等,我正在做某事”的通用符号

  • 如果您有任何不确定的轮询,并且您正在使用 ProgressBar,尝试将其用作不确定的 ProgressBar,或旋转器等。 WPF 有一个内置属性可以使进度条不确定,这非常有用。

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.

(As has been mentioned, if they're 10% through, send 10, if they're 50% through, send 50)

Using a BackgroundWorker bgWrk

Add the following event:

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);

A couple of notes:

  • You can pass an Object as well in the ReportProgress() 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"

  • 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.

瀟灑尐姊 2024-09-01 12:12:00

有多少步?如果有 10 个步骤,只需在成功完成的每个步骤的末尾使用 10% 的步骤标记即可。

How many steps is there? If there's 10 steps, simply use 10% step up marker for the end of each step that completed successfully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文