private void btnSearch_Click(object sender, EventArgs e) { this.progressBar1.Maximum = 9000; progressBar1.Minimum = 0; progressBar1.MarqueeAnimationSpeed = 1000; Thread tt = new Thread(new ThreadStart(LongProcess)); tt.Start(); tt.Join(); } private void LongProcess() { for (int i = 0; i < 9000; i++) {if (InvokeRequired) { BeginInvoke(new UpProgressDelegate(LongProcess)); return; } this.progressBar1.Value = i; }}进度条在运行时,其它窗体控件不能使用...只能等到进度条运行完后,才能使用,我这里开线程了,还有就是我回到UI所在线程修改控件是不是造成卡死的原因啊,请高手指点啊,并给出好的意见,最好给个小例子!可以追加分数!
3 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
去掉tt.Join();
因为tt.Join()阻塞了主线程,所以界面会卡.
可以这样写代码:
private void Form1_Load(object sender, EventArgs e)
{
ProgressBar.CheckForIllegalCrossThreadCalls = false;
}
private void btnSearch_Click(object sender, EventArgs e)
{
this.progressBar1.Maximum = 9000;
progressBar1.Minimum = 0;
progressBar1.MarqueeAnimationSpeed = 1000;
Thread tt = new Thread(new ThreadStart(LongProcess));
tt.Start();
// tt.Join();
}
private void LongProcess()
{
for (int i = 0; i < 9000; i++)
{
this.progressBar1.Value = i;
}
}
白板的微信
TA贡献1883条经验 获得超3个赞
下面是典型的多线程操作界面的例子,我按照你的要求改了,已经测试过,
界面不会卡住
private void buttonSearch_Click(object sender, EventArgs e)
{
this.progressBar1.Maximum = 9000;
progressBar1.Minimum = 0;
progressBar1.MarqueeAnimationSpeed = 1000;
Thread tt = new Thread(new ThreadStart(LongProcess));
tt.IsBackground = true;
tt.Start();
}
delegate void UpProgressDelegate(int value);
void ProgressIncrease(int value)
{
this.progressBar1.Value = value;
}
void LongProcess()
{
for (int i = 0; i < 9000; i++)
{
//Do something
Thread.Sleep(10); //在这里做一些需要在线程中做的长时间操作,这里用休眠10ms来模拟。
//Porogress Increase
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new UpProgressDelegate(ProgressIncrease),
new object[] { i });
}
else
{
ProgressIncrease(i);
}
}
}
- 3 回答
- 0 关注
- 427 浏览
添加回答
举报
0/150
提交
取消