为什么偶的进度条的Value为100了 而显示出来进度条才走到中间
偶的思路是这样的
首先开启定时器 让进度条滚动 然后用BeginInvoke启动一个耗时的操作。
定时器最多可以将滚动条滚到到 倒数第一步
耗时操作的最后才将滚动条滚到最终的位置
有些时候又很正常
有些时候就出现如标题所描述的情况?
当整个过程结束以后 进度条的Value为100了 而显示出来进度条才走到中间
同事们研究了半天 说是进度条的BUG
代码如下
private void button1_Click(object sender, System.EventArgs e)
{
progressBar1.Value = 0;
progressBar1.BeginInvoke(
new MethodInvoker(DoSth));
timer = new System.Timers.Timer();
timer.AutoReset = true;
timer.Interval = 200;
timer.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
private void DoSth()
{
string path = "D:\\a.dot";
Work work = new Work();
work.GetFieldsValues(path);
timer.Stop();
while(progressBar1.Value < progressBar1.Maximum)
progressBar1.PerformStep();
//这里显示已经100了
MessageBox.Show(progressBar1.Value.ToString());
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(progressBar1.Value < progressBar1.Maximum -1)
progressBar1.PerformStep();
}
{
progressBar1.Value = 0;
progressBar1.BeginInvoke(
new MethodInvoker(DoSth));
timer = new System.Timers.Timer();
timer.AutoReset = true;
timer.Interval = 200;
timer.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
private void DoSth()
{
string path = "D:\\a.dot";
Work work = new Work();
work.GetFieldsValues(path);
timer.Stop();
while(progressBar1.Value < progressBar1.Maximum)
progressBar1.PerformStep();
//这里显示已经100了
MessageBox.Show(progressBar1.Value.ToString());
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(progressBar1.Value < progressBar1.Maximum -1)
progressBar1.PerformStep();
}