问题描述
我对进度条显示值有疑问.
I have question about progressbar show value.
我有这个主线程
private void button1_Click(object sender, EventArgs e){进度编 = 新进度();progress.progressEvent += 新的progress.progressEventHandler(progressEvent);for(int i=0;i
private void button1_Click(object sender, EventArgs e){progress prog = new progress();progress.progressEvent += new progress.progressEventHandler(progressEvent);for(int i=0;i<100;i++){Thread.Sleep(100);prog.incA();}}
void progressEvent(对象发送者){如果(progressBar1.InvokeRequired){//Tady mi to caka az kym nedobehne cyklus for a pak zacne tohleto fungovatprogressBar1.Invoke(new ChangeProgressBarValue(ProgressStep));}别的{进度步();}
void progressEvent(object sender){if (progressBar1.InvokeRequired){//Tady mi to caka az kym nedobehne cyklus for a pak zacne tohleto fungovatprogressBar1.Invoke(new ChangeProgressBarValue(ProgressStep));}else{ProgressStep();}}
public void ProgressStep(){progressBar1.PerformStep();
public void ProgressStep(){progressBar1.PerformStep();}
公开课进度{私人线程开始 ts;私人线程 th;私人布尔状态=真;公共委托 void progressEventHandler(object sender);公共静态事件progressEventHandler progressEvent;私有整数 b,a = 0;
public class progress{private ThreadStart ts;private Thread th;private bool status = true;public delegate void progressEventHandler(object sender);public static event progressEventHandler progressEvent;private int b,a = 0;
公共进步(){ts=new ThreadStart(go);th = 新线程(ts);th.IsBackground = true;th.Start();
public progress(){ts=new ThreadStart(go);th = new Thread(ts);th.IsBackground = true;th.Start();}
public void incA(){一个++;如果(a==100)状态 = 假;
public void incA(){a++;if(a==100)status = false;}
private void go(){而(状态){如果 (a != b){b = a;如果(进度事件!= null)进度事件(这个);}}th.Abort();}}
private void go(){while (status){if (a != b){b = a;if (progressEvent != null)progressEvent(this);}}th.Abort();}}
我的问题是如果启动主线程并调用IncA这个方法调用事件,事件是progressbar invoke这个调用等待结束主线程 FOR
and my problem is IF start main thread and call IncA this method call event and in event is progressbar invokeand this invoke waiting to end main thread FOR
为什么要等?谢谢
推荐答案
您在主线程中的循环阻止了绘制"事件的发生.由于您是通过该线程调用进度条的函数,因此您将永远看不到更新.
Your loop in the main thread is preventing "paint" events from happening. Since you are calling your progress bar's function from withing that thread, you will never see the updates.
您需要移动代码以将增量完全转移到另一个线程.
You need to move the code to do the incrementing to another thread entirely.
以下是您想要使用 Button
、BackgroundWorker
和 ProgressBar
执行的操作的示例:
Here is a sample of what you want to do using a Button
, a BackgroundWorker
, and a ProgressBar
:
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
backgroundWorker1.ReportProgress(i);
Thread.Sleep(100);
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
希望这会有所帮助!
这篇关于进度条独立线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!