从另一个线程和更新类在C#GUI进度条 [英] Updating a progress bar in a C# GUI from another thread and class

查看:220
本文介绍了从另一个线程和更新类在C#GUI进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
从另一个线程






<更新进度条p >在我的计划,我想非GUI功能分离到另一个类,并留在主类相关的GUI的东西。不过,我有更新进度条,而在工人阶级中的工人的方法之一是做了工作的问题。我知道,我将与多线程在这里工作,但我不明白怎么。我可能只是缺少简单的事情,但是当我寻找关于它的信息,似乎大多数的教程谈细节,但不解释大局很好。我部分地理解调用和委托命令,但我真的不明白他们是如何进行互动。



下面是剥离下来的是我想要做的版本。我如何修改这个来更新进度条,但保留窗口响应和重新粉刷



主窗体类:

 公共部分Form1类:表格
{
time_waster的事情=新time_waster();

公共Form1中()
{
的InitializeComponent();
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
thing.something_that_takes_a_while();
}
}



单独的工作类:
类time_waster
{
公共time_waster()
{
}

 公共无效something_that_takes_a_while( )
{
INT延迟= 200;
的for(int i = 0; I< 100;我++)
{
Thread.sleep代码(延迟);
//适当的代码来更新循环的每次迭代进度条。
}
}
}


解决方案

.NET包含一个名为 BackgroundWorker的,它提供了用于报告后台线程中的事件的进展的方法。该事件被自动调用它创造了BackgroundWorker的(通常是UI线程)的线程上。



订阅了ProgressChanged事件,并更新进度条事件处理程序。官方的MSDN文档提供了一些示例代码。


Possible Duplicate:
Updating a Progress Bar from Another Thread

In my program, I wanted to separate non-GUI functions to another class, and leave things related to the GUI in the main class. However, I am having issues with updating a progress bar while one of the worker methods in the worker class is doing its job. I know that I will have to work with multithreading here, but I do not understand how. I may just be missing simple things, but when I look for information about it, it seems that most tutorials talk about the minutia, but do not explain the big picture very well. I partially understand what invoke and delegate commands are, but I don't really understand how they interact.

Below is stripped down version of what I want to do. How do I modify this to update the progress bar, but keep the window responsive and repainting?

Main form class:

public partial class Form1 : Form
{
    time_waster thing = new time_waster();

    public Form1()
    {
        InitializeComponent();
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        thing.something_that_takes_a_while();
    }
}

Separate worker class: class time_waster { public time_waster() { }

    public void something_that_takes_a_while()
    {
        int delay = 200;
        for (int i = 0; i < 100; i++)
        {
            Thread.Sleep(delay);
            //appropriate code to update the progress bar for each iteration of the for loop.
        }
    }
}

解决方案

.NET Includes a class called the BackgroundWorker, which provides methods for reporting the progress of the background thread in an event. The event is automatically called on the thread which created the BackgroundWorker (typically, the UI thread).

Subscribe to that "ProgressChanged" event, and update the progress bar in that event handler. The official MSDN Documentation provides some sample code.

这篇关于从另一个线程和更新类在C#GUI进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆