如何更新进度条一步,每一个循环周期? C# [英] How do I update the progress bar one step, every loop cycle? C#

查看:174
本文介绍了如何更新进度条一步,每一个循环周期? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建在C#中,Windows窗体.NET应用程序。如何更新进度条1步100周期循环的每个周期?
(我处理一个Excel工作表中的循环。)
进度条控件是在它连接到它连接到一个自定义类
(MVC模式)的控制器类的UI类。循环是在自定义类。
我需要一路下行发送UI类实例中的每个方法或是否有更好的办法吗?

Creating a .net application in C#, windows forms. How do I update the progress bar 1 step every cycle of a 100 cycle loop? (I’m processing an excel sheet in the loop.) The progress bar controls are in the UI class which connects to the controller class which connects to a custom class (MVC pattern). The loop is in the custom class. Do I need to send the UI class instance all the way down in each method or is there a better way?

眼下之后进度条更新循环完成。 Application.doevents和.update或.REFRESH不起作用。

Right now the progress bar updates after the loop finishes. Application.doevents and .update or .refresh don’t work.

推荐答案

说出下面是你们班负责做的工作在它的循环。添加事件,表明你的进步。然后从你的用户界面简单处理该事件,并相应地更新进度条。

Say the below is your class responsible for doing the work with the loop in it. Add an event to indicate your progress. Then from your UI simply handle that event and update the progress bar accordingly.

sealed class Looper
{
    public event EventHandler ProgressUpdated;

    private int _progress;
    public int Progress
    {
        get { return _progress; }
        private set
        {
            _progress = value;
            OnProgressUpdated();
        }
    }

    public void DoLoop()
    {
        _progress = 0;
        for (int i = 0; i < 100; ++i)
        {
            Thread.Sleep(100);
            Progress = i;
        }
    }

    private void OnProgressUpdated()
    {
        EventHandler handler = ProgressUpdated;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
}

您可以通过让实现这个一个的BackgroundWorker 为你的用户界面,在这里你叫 backgroundWorker.DoWork 事件尺蠖的一部分.DoLoop()。然后在你的处理程序 looper.ProgressUpdated 事件中,你可以叫 backgroundWorker.ReportProgress 从UI增加你的进度条主题。

You might implement this by having a BackgroundWorker as part of your UI, where in the backgroundWorker.DoWork event you call looper.DoLoop(). Then in your handler for the looper.ProgressUpdated event you can call backgroundWorker.ReportProgress to increment your progress bar from the UI thread.

请注意,它可能会更有意义,包括通过所携带的信息进步本身的 ProgressUpdated 事件(我只是不喜欢从 EventArgs的写出一个新的类派生来说明这一点;你可能得到的图片反正)。

Note that it would probably make more sense to include the progress itself in the information carried by your ProgressUpdated event (I just didn't feel like writing out a new class deriving from EventArgs to illustrate this; you probably get the picture anyway).

另外请注意,除非你对从UI线程一个单独的线程循环执行代码上面确实没有意义。否则,所有的工作都花在做下一个UI刷新反正之前,所以你的进度条将不会提供任何值(它只是从0到100去当循环完成)。

Also note that the above really doesn't make sense unless you're executing your code with the loop on a separate thread from the UI thread. Otherwise, all of your work is getting done before the next UI refresh anyway, so your progress bar would not be providing any value (it would just go from 0 to 100 when the loop completed).

的这种事怎么可以实现只是一个例子。

Just an example of how this sort of thing can be achieved.

这篇关于如何更新进度条一步,每一个循环周期? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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