C# - 如何正确使用进度条(GUI的) [英] C# - How to properly use progress bars (GUI's)

查看:641
本文介绍了C# - 如何正确使用进度条(GUI的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是一个沉默的程序,或者拥有一个控制台,我想作一个进度条来跟踪每一个事件我做的状态。

Instead of a silent program, or one with a console, I wanted to make a progress bar to track the status of each event I am doing.

活动:


  1. 阅读/解析文件

  2. 阅读Excel文件

  3. 数据库
    查询

等...

现在我完成这些任务我增加了进度条来表示它已经完成了一个,我写一个DataGridTable,状态已经从计算...来完成。后。

Now after I complete one of these tasks I increment the progress bar to indicate that it has completed and I write to a DataGridTable that the status has changed from 'Calculating...' to 'Done.'.

有关简单说我有一个文件compute.cs和form.cs.我的靴子的形式和我preSS执行运行该脚本compute.cs,它通过转发其状态返回到表单:

For simplicity say I have a file compute.cs and form.cs. My form boots up and I press 'execute' running the compute.cs script, which relays its status back to the form by:

compute.cs

form.SetStatus(EVENTID,eventStatus);

form.SetStatus(eventId, eventStatus);

form.cs

委托无效SetMetricStatusDelegate(字符串指标,串状态);

delegate void SetMetricStatusDelegate(string metric, string status);

公共无效SetMetricStatus(字符串指标,串状态){...}

public void SetMetricStatus(string metric, string status) { ... }

最后,该问题。我跑我的程序,每个任务需要大约三秒钟即可完成,但进度条和表未更新。在所有的活动已经完成,那么进度条和表更新正确的。

And finally, the problem. I run my program, each task takes around three seconds to complete, but the progress bar and table is not updating. After all the events have completed, then the progress bar and table update correctly.

什么是使用一个进度条C#的形​​式,记录进步的正确方法?

What is the proper way to record progress using a progress bar on a C# form?

推荐答案

而长时间运行的任务执行你的UI涂料暂停。

Your UI paint is pausing while the long running tasks are performed.

您需要产生一个线程从UI分开,在执行长的任务。

You need to spawn a thread separate from the UI to perform the long tasks in.

请参阅:<一href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx\">BackgroundWorker类

要注意的是从一个没有创建它们会抛出异常一个线程访问UI控件。幸运的是,BackgroundWorker的具有您可以订阅当线程完成的事件。

Beware that accessing UI controls from a thread that did not create them will throw exceptions. Fortunately, the BackgroundWorker has events that you can subscribe to when the thread is complete.

这篇关于C# - 如何正确使用进度条(GUI的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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