多线程中子线程控制进度条(参阅msdn的)
2006-06-19 11:21
288 查看
private void button1_Click(object sender, System.EventArgs e)
{
flag=false;
Thread thread=new Thread(new ThreadStart(target));
thread.IsBackground=true;
thread.Start();
}
public void target()
{
try
{
MethodInvoker mi=new MethodInvoker(countPro);
while(!flag)
{
this.Invoke(mi);
Thread.Sleep(1000);
}
}
catch (ThreadInterruptedException e)
{
if (e != null) {}
}
catch (Exception we)
{
if (we != null)
{
//MessageBox.Show(we.ToString());
}
}
}
public void countPro()
{
int min;
double numerator,denominator,completed;
min=this.progressBar1.Minimum;
if(this.progressBar1.Value==this.progressBar1.Maximum)
{
this.progressBar1.Value=this.progressBar1.Minimum;
}
else
{
this.progressBar1.PerformStep();
}
//显示进度
this.textBox1.Text=this.progressBar1.Value.ToString();
numerator=this.progressBar1.Value-min;
denominator=this.progressBar1.Maximum-this.progressBar1.Minimum;
completed=(numerator/denominator)*100.0;
this.label2.Text=Math.Round(completed)+"%";
}
private void button2_Click(object sender, System.EventArgs e)
{
flag=true;
}
{
flag=false;
Thread thread=new Thread(new ThreadStart(target));
thread.IsBackground=true;
thread.Start();
}
public void target()
{
try
{
MethodInvoker mi=new MethodInvoker(countPro);
while(!flag)
{
this.Invoke(mi);
Thread.Sleep(1000);
}
}
catch (ThreadInterruptedException e)
{
if (e != null) {}
}
catch (Exception we)
{
if (we != null)
{
//MessageBox.Show(we.ToString());
}
}
}
public void countPro()
{
int min;
double numerator,denominator,completed;
min=this.progressBar1.Minimum;
if(this.progressBar1.Value==this.progressBar1.Maximum)
{
this.progressBar1.Value=this.progressBar1.Minimum;
}
else
{
this.progressBar1.PerformStep();
}
//显示进度
this.textBox1.Text=this.progressBar1.Value.ToString();
numerator=this.progressBar1.Value-min;
denominator=this.progressBar1.Maximum-this.progressBar1.Minimum;
completed=(numerator/denominator)*100.0;
this.label2.Text=Math.Round(completed)+"%";
}
private void button2_Click(object sender, System.EventArgs e)
{
flag=true;
}
相关文章推荐
- C# 通过线程来控制进度条(转)--讲解多线程对界面的操作
- c#中子线程控制进度条的一个简单例子(多线程问题)
- 应用select 函数控制多线程中子线程结束方法
- 应用select 函数控制多线程中子线程结束方法
- 应用select 函数控制多线程中子线程结束方法
- [Python]多线程网址爬虫:控制线程数,爬虫深度
- 多线程-线程控制之休眠线程
- 多线程-线程控制之守护线程
- JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)
- (45)Java学习笔记——多线程 / 设置线程 / 线程调度 /线程控制
- 多线程-3-线程控制
- java中结合单例模式,控制单例类的某方法在被多线程调用时,只被1个线程调用执行,执行完毕后才被另一线程调用
- 多线程入门-第三章-线程的调度与控制之优先级
- SE高阶(4):多线程(并发)—①创建启动方式和控制线程方法
- [简单多线程控制]_[模拟杀毒软件线程控制]
- c#中子线程控制进度条的一个简单例子
- Java多线程(二)、线程的生命周期和状态控制
- 7.多线程学习--处理线程的不受控制异常
- 多线程——控制线程状态
- 多线程---使用ManualResetEvent来控制线程间的同步(实现了消费者和生产者模式)