如何使用进度条 [英] how to use a progress bar

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

问题描述

此代码无法正确执行.

我需要运行此进度条,但在运行时不会增加该值.
请帮帮我!

This code doesn''t execute properly.

I need to run this progress bar, but in run time it doesn''t increase the value.
Please help me!

if (progressBar1.Value < progressBar1.Maximum)
      progressBar1.Value = progressBar1.Value + 1;



如果我使用"while"而不是"if",则进度条可以正常工作,但是进度条会很快填满.

如何减慢进度条的填充速度?


2台笔记本电脑上运行2个应用程序.
一个应用程序将一个值传递给另一个.
Application1将no传递给application2,并且当application2从application1接收到no时,变量状态设置为0(变量状态"在applicatio2中),否则状态设置为1
(变量状态"设置为1,直到它从application1接收到否,一旦application2从application1接收到否,变量状态"就更改为0.).

进度条应在application2中运行,直到它从application1接收到否为止(application2中的进度条应运行,直到变量状态"从1变为0)
(只要状态"等于1,进度条就应该运行).



If I use "while" instead of "if" then the progress bar works properly, but the bar fills up so quickly.

How can I slow down the speed of filling the progressBar?


There are 2 applications running on 2 laptops.
One application passes a value to the other.
Application1 pass a no to application2, and when application2 receives the no from application1, variable state is set to 0 (variable "state" is in the applicatio2)otherwise state is set to 1
(variable "state" is set to 1 until it receives the no from application1,as soon as application2 receives the no from application1 variable "state" is changed to 0 ).

The progress bar should run in application2 until it receives the no fromapplication1(the progress bar in application2 should run until the variable "state" changes from 1 to 0)
(simply the progress bar should run as long as "state" is equal to 1)

推荐答案

通常,进度条用于指示任务已进行了多长时间,从而使用户了解完成需要多长时间.

如果将if替换为while:
Normally, a progress bar is used to indicate how far a task has progressed, and thus to give the user an idea how long it will take to complete.

If you replace the if with while:
while (progressBar1.Value < progressBar1.Maximum)
      progressBar1.Value = progressBar1.Value + 1;

,然后可以有效地将其作为单个语句执行:

then it is effectively executed as a single statement:

progressBar1.Value = progressBar1.Maximum



您通常会执行的操作是在任务开始时启动进度条,并在每次完成重要工作时增加其价值.例如,如果您要处理十个文件,则最大值可能是十个,并且在处理完每个文件后将增加Value属性.

但是,如果使用进度条,则通常在单独的线程中进行处理,以使用户界面美观流畅-唯一的问题是您需要在处理线程中调用控件,因为它无法访问UI控件.
如果使用System.ComponentModel.BackgroundWorker,则无需进行调用.
BackgroundWorker自行提供跨线程边界的状态报告.
1.将BackgroundWorker.WorkerReportsProgress属性设置为true.
2.在工作程序例行调用((BackgroundWorker)sender).ReportProgress()中.
3. UI是否在BackgroundWorker.ProgressChanged事件处理程序中更改(包括进度栏).



What you would normally do it to start the progress bar at the beginning of a task, and increase the value each time you had done some significant part of the work. For example, if you were processing ten files, the the maximum value might be ten, and you would increase the Value property after each file was processed.

However, if you are using a progress bar, then normally the processing is done in a separate thread, to get the User Interface nice and fluid - the only problem being that you need to Invoke controls in the processing thread as it cannot access UI controls.
If you use a System.ComponentModel.BackgroundWorker, there''s no need for you to do the invoking.
The BackgroundWorker offers status reporting across thread bounderies on its own.
1. Set the BackgroundWorker.WorkerReportsProgress property to true.
2. Within the worker routine call ((BackgroundWorker)sender).ReportProgress().
3. Do the UI changes (including progress bar) within BackgroundWorker.ProgressChanged event handler.


是否可以确定应用程序1向应用程序2发送内容所需的时间?因为那是使用进度条的时间:您可以估计时间或操作量的时间.它向用户显示完成整个工作的哪一部分以及用户需要等待多少时间.
但是,如果您的应用程序2可以在未确定的时间内等待应用程序1,则无法使用进度条.当您只是不知道程序(和用户)实际需要等待多少时间时,如何告诉用户已经过了30%的等待时间?
另一方面,如果您只想向用户显示您的应用程序正在等待,则情况会发生变化.有可以使用的控件,最简单的实际上是进度条.我猜它应该很快就填满,然后变空(将进度设置为零),再次填满...等等.在这种情况下,使用与所示类似的代码,只需将if 更改为while并在其中放置一些延迟-等待事件,或者如lukeer所建议的那样,等待Sleep().如果您在同一位置使用进度条执行等待应用程序1"功能,请考虑在此等待应用程序1"周期(如果有)中更新进度条.
Is there any way to determine the time required for application1 to send something to application2? Because that''s when progress bar is used: when you can estimate time or amount of operations. It shows the user which part of the whole work is done and how much the user has to wait.
If, however, your application2 can wait for application1 for undetermined amount of time, progress bar cannot be used. How can you tell the user that, say, 30% of the wait time has passed when you just don''t know how much your program (and user) shall actually wait?
On the other hand, if you just want to show the user that your application is waiting right now, it changes the situation. There are controls that can be used, the simplest is actually the progress bar. It should fill up rather quickly, I guess, then become empty (set progress to zero), fill up again... and so on. In this case use the code similar to shown, just change if to while and put some delay inside - wait for event or, as lukeer suggests, Sleep(). If you perform "wait for application1" functionality in the same place with progress bar, consider updating progress bar in this "wait for application1" cycle (if any)


如果您想通知用户当前正在发生某事,但是您不知道会持续多久,可以设置
ProgressBar1.Style = ProgressBarStyle.Marquee;
这将显示出不错的动画,而无需您进行任何更新.
If you want to inform the user that something is going on at the moment, but you don''t know exactly how long it is going to last, you can set
ProgressBar1.Style = ProgressBarStyle.Marquee;
This will show a nice animation without you needing to update anything.


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

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