progressBar刷新问题?

zengbingling 2011-08-24 05:15:38
public partial class Form1 : Form
{
Worker worker;
object itemSycn;
public Form1()
{
InitializeComponent();
worker = new Worker(new EventHandler(worker_activedThreadChanged);
}

private void btnRun_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 100;
progressBar1.Value = 0;
worker.Work();
}

void worker_activedThreadChanged(object sender, EventArgs e)
{
progressBar1.Increment(1);
}
}
public class Worker
{
SimpleThreadPool threadPool;
EventHandler activedThreadChanged;
public Worker(EventHandler threadChagned)
{
threadPool = new SimpleThreadPool();
this.activedThreadChanged=threadChagned;
threadPool.ThreadCountChanged += new EventHandler(threadPool_ThreadCountChanged);
}
void threadPool_ThreadCountChanged(object sender, EventArgs e)
{
if (activedThreadChanged != null)
activedThreadChanged(sender, e);
}
void Work()
{
//执行线程池任务。一个任务完成时会执行threadPool_ThreadCountChanged。
}
}

问题:当执行完一个任务时,进度条数值变了但界面不刷新。执行完所有任务才显示进度完成?
...全文
171 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pcqpzq 2011-08-24
  • 打赏
  • 举报
回复
后台线程不要直接更改UI,请用Form1.Invoke()方法,具体百度
636f6c696e 2011-08-24
  • 打赏
  • 举报
回复
请在threadPool_ThreadCountChanged里,强制刷新progressbar
progressBar1.Refresh();

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧