Application.DoEvents用法

dinner888 2009-12-22 01:13:32
我想在程序没有加载完成时,显示一个正在加载的对话框
private void Form1_Load(object sender, EventArgs e)
{
//提示窗体,显示正在加载.....
DoEventsForm doEventsForm = new DoEventsForm();
doEventsForm.Show();
this.Visible = false;
Application.DoEvents();

....处理复杂运算业务消耗时间较长

//关闭提示窗体,显示本窗体
doEventsForm.Dispose();
this.Visible = true;
}

为何这样写,感觉好象被卡住一样.
...全文
2332 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
anniekirea 2010-12-20
  • 打赏
  • 举报
回复
个人感觉 Application.DoEvents(),是针对此句前面所执行的代码段中存在的最近的触发事件的一个跟踪
vinihao 2010-09-19
  • 打赏
  • 举报
回复
我是第一次用,借鉴了
calizy 2010-08-26
  • 打赏
  • 举报
回复
学习了
qqliukk2123 2010-04-08
  • 打赏
  • 举报
回复
偶也是只用不懂其道理啊!今天看了,晚上写个例子看看呗。呵呵!
doubleu2005 2010-04-02
  • 打赏
  • 举报
回复
Application.DoEvents();//实时响应文本框中的值
学习
dinner888 2009-12-24
  • 打赏
  • 举报
回复
怎么没人说话啊???
十八道胡同 2009-12-24
  • 打赏
  • 举报
回复
记得第一次使用Application.DoEvents()是为了在加载大量数据时能够有一个数据加载的提示,不至于系统出现假死的现象,当时也没有深入的去研究他的原理是怎样的,结果在很多地方都用上了Application.DoEvents(),今天看到了关于这方面的一些文章,知道我以前有些用法是不当的,有些地方需要慎用Application.DoEvents()。
首先我们先看看在循环比较大的程序中,它的作用还是不错的,起到了一个实时响应的效果,例如:


for (int q = 0; q < 1000000; q++)
{
textBox1.Text = q.ToString();
Application.DoEvents();//实时响应文本框中的值
}如果没有加上 DoEvents的话,由于循环时间会比较久就会出现假死的状态,而且程序不能处理其他的事件。而如果加上DoEvents的话就会对文本框的值实时响应,给用户带来较好的用户体验,可是DoEvents也带来了效率上的问题,处理同样的一个事件调用了DoEvents后效率降低了好几倍,这也是为什么要慎用的原因了。
http://www.cnblogs.com/datong/archive/2008/04/06/1139216.html
silentwins 2009-12-24
  • 打赏
  • 举报
回复
private void InitializePictureBox()
{
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.PictureBox1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
this.PictureBox1.Location = new System.Drawing.Point(72, 112);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(160, 136);
this.PictureBox1.TabIndex = 6;
this.PictureBox1.TabStop = false;
}

private void InitializeOpenFileDialog()
{
this.OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();

// Set the file dialog to filter for graphics files.
this.OpenFileDialog1.Filter =
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
"All files (*.*)|*.*";

// Allow the user to select multiple images.
this.OpenFileDialog1.Multiselect = true;
this.OpenFileDialog1.Title = "My Image Browser";

}

private void fileButton_Click(System.Object sender, System.EventArgs e)
{
OpenFileDialog1.ShowDialog();
}


// This method handles the FileOK event. It opens each file
// selected and loads the image from a stream into PictureBox1.
private void OpenFileDialog1_FileOk(object sender,
System.ComponentModel.CancelEventArgs e)
{

this.Activate();
string[] files = OpenFileDialog1.FileNames;

// Open each file and display the image in PictureBox1.
// Call Application.DoEvents to force a repaint after each
// file is read.
foreach (string file in files )
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
System.IO.FileStream fileStream = fileInfo.OpenRead();
PictureBox1.Image = System.Drawing.Image.FromStream(fileStream);
Application.DoEvents();
fileStream.Close();

// Call Sleep so the picture is briefly displayed,
//which will create a slide-show effect.
System.Threading.Thread.Sleep(2000);
}
PictureBox1.Image = null;
}
盟主 2009-12-24
  • 打赏
  • 举报
回复
不会啊,我这菜鸟也是来看看咋用的,因为我也遇到此问题了~
dinner888 2009-12-22
  • 打赏
  • 举报
回复
给个例子,谢谢
wuyq11 2009-12-22
  • 打赏
  • 举报
回复
多线程backgroundworker
Application.DoEvents()是为了在加载大量数据时能够有一个数据加载的提示,不至于系统出现假死的现象
可确保辅助线程激发的任何事件都由该 UI 线程处理
freeboy827 2009-12-22
  • 打赏
  • 举报
回复
新开一个线程比较好
用backgroundworker
liherun 2009-12-22
  • 打赏
  • 举报
回复
你这样还不如新开个线程了

111,120

社区成员

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

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

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