如何在 Windows 窗体应用程序中使用多线程来更新进度条?

发布于 2024-08-27 21:07:44 字数 428 浏览 9 评论 0原文

有两个对象。带有按钮和进度条的 Windows 窗体,以及另一个处理算法的对象。

在算法对象中有一个事件和一个属性。事件是 ProgressChanged,属性是 Progress(它是一个 int)。

在调用窗口中,该按钮启动算法对象中的一组步骤。当每个步骤(或子步骤)发生时,都会触发 ProgressChanged 事件,并且窗口中存在一个事件处理程序,该处理程序实质上会相对于 Progress 属性递增进度条。

我遇到的问题是,因为该算法有可能(并且很可能)运行相对较长的时间,所以我需要将其移动到它自己的后台线程中,并将事件推回窗口。

我的问题是,当涉及到多线程时,我不完全确定我在做什么。我查看了 Control.Invoke 并且有点迷失。

有人能指出我正确的方向吗?

There are two objects. The Windows Form with a button and a progress bar, and another object that handles an algorithm.

In the algorithm object there is an event, and a property. The event is ProgressChanged, and the property is Progress (which is an int).

In the calling window, the button starts off a set of steps in the algorithm object. As each step (or substeps) occurs, the ProgressChanged event fires, and in the window there is an event handler that essentially increments the progress bar relative to the Progress property.

The problem I am running into is that because the algorithm has a possibility (and high liklihood) of running a relatively long time, I need to move it into it's own background thread and push the event back up to the window.

My issue is that I'm not completely sure what I'm doing when it comes to multi-threading. I've looked at Control.Invoke and I'm a little lost.

Can someone point me in the right direction?

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

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

发布评论

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

评论(3

妄断弥空 2024-09-03 21:07:44

使用后台工作者。它有一个 ReportProgress 方法 + 事件,让您运行进度条而无需担心 Invoke。

Use a Backgroundworker. It has a ReportProgress method + event to let you run the progressbar w/o worrying about Invoke.

吖咩 2024-09-03 21:07:44

下面是如何使用 Invoke 更改到 UI 线程的示例,在该线程中可以安全地访问控件:

// In some form method, setup the event handler
algorithm.ProgressChanged += new EventHandler(AlgorithmProgressChanged);

private void AlgorithmProgressChanged(Object source, EventArgs args)
{
    if (this.InvokeRequired)
    {
        // Switch to the UI thread
        this.Invoke(new EventHandler(AlgorithmProgressChanged), source, args);
        return;
    }
    // It should be safe to set the progress beyond this point.
    progressBar.Value = algorithm.Progress;
}

Here's an example of how to use Invoke to change to the UI thread, where it is safe to access controls:

// In some form method, setup the event handler
algorithm.ProgressChanged += new EventHandler(AlgorithmProgressChanged);

private void AlgorithmProgressChanged(Object source, EventArgs args)
{
    if (this.InvokeRequired)
    {
        // Switch to the UI thread
        this.Invoke(new EventHandler(AlgorithmProgressChanged), source, args);
        return;
    }
    // It should be safe to set the progress beyond this point.
    progressBar.Value = algorithm.Progress;
}
澜川若宁 2024-09-03 21:07:44

正如另一张海报所说,使用 BackgroundWorker 组件。说明和示例代码: http://ondotnet.com/ pub/a/dotnet/2005/07/25/backgroundworker.html

-Oisin

As the other poster says, use a BackgroundWorker component. Explanation and sample code on: http://ondotnet.com/pub/a/dotnet/2005/07/25/backgroundworker.html

-Oisin

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