大批量计算UI显示问题

godman 2008-05-05 03:56:06
在程序中有一个大量计算,在WinForm中显示状态
当计算量一大WinForm还是呈现死机状态
但是加上Thread.Sleep(1)就可以显示
有个问题啊,加上Thread.Sleep(1)程序处理速度就慢了呀

在我的button click事件中代码如下

private void btnStep1_Click(object sender, EventArgs e)
{
ReadExcelDelegate red = new ReadExcelDelegate(ReadExcel);
red.BeginInvoke(null, null);
}



显示有问题的程序

private delegate void ReadExcelDelegate();
private void ReadExcel()
{
for (int i = 0; i < 100000; i++)
{
i = i + 1;

ShowProgress(i.ToString());
}
}

//UI显示
delegate void ShowProgressDelegate(string con);
void ShowProgress(string con)
{
// 确保在正确的线程上
if (this.textBox1.InvokeRequired == false)
{
this.textBox1.Text = con;
}
else
{
// 异步显示进度
ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
this.textBox1.BeginInvoke(showProgress, new object[] { con });
}
}


显示没有问题的代码

private delegate void ReadExcelDelegate();
private void ReadExcel()
{
for (int i = 0; i < 100000; i++)
{
i = i + 1;

Thread.Sleep(1);

ShowProgress(i.ToString());
}
}

//UI显示
delegate void ShowProgressDelegate(string con);
void ShowProgress(string con)
{
// 确保在正确的线程上
if (this.textBox1.InvokeRequired == false)
{
this.textBox1.Text = con;
}
else
{
// 异步显示进度
ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
this.textBox1.BeginInvoke(showProgress, new object[] { con });
}
}


谁能给个好的解决方案阿,我不想在程序中用Thread.Sleep(1)
本人用的2003
...全文
110 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
godman 2008-05-05
  • 打赏
  • 举报
回复
Application.DoEvents();//试试
我试过,不行的

[Quote=引用 2 楼 hanjun1024 的回复:]
this.textBox1.BeginInvoke换成this.textBox1.Invoke试试看
[/Quote]

这个可以实现
谢谢各位
hanjun1024 2008-05-05
  • 打赏
  • 举报
回复
this.textBox1.BeginInvoke换成this.textBox1.Invoke试试看
朝圆夜不圆 2008-05-05
  • 打赏
  • 举报
回复
private delegate void ReadExcelDelegate();
private void ReadExcel()
{
for (int i = 0; i < 100000; i++)
{
i = i + 1;
System.Windows.Forms.Application.DoEvents();//试试

ShowProgress(i.ToString());
}
}

//UI显示
delegate void ShowProgressDelegate(string con);
void ShowProgress(string con)
{
// 确保在正确的线程上
if (this.textBox1.InvokeRequired == false)
{
this.textBox1.Text = con;
}
else
{
// 异步显示进度
ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
this.textBox1.BeginInvoke(showProgress, new object[] { con });
}
}

110,567

社区成员

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

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

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