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

问题描述

此代码无法正确执行.

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

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)

推荐答案

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.




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

08-18 20:42