Application.DoEvent使用(转载)

[size=medium]Application.DoEvent使用(转载)[/size]//msdn,有这样一个例子:

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;
}


在private void openFileDialog1_FileOk方法中使用了此方法,况且在紧跟其后有个延时,延时了2秒,不明白此句的用意。多次运行程序发现使用了Application.DoEvent()后图片能够实时显示,文件框在隔2秒后关闭,屏蔽Application.DoEvent()后发现图片不能实时显示,在隔两秒文件框关闭时显示图片。此时明白了Application.DoEvent()的用意,是使界面能够实时刷新,感觉有点多线程的意思。

继续查资料查看对此语句的详细介绍,下面是从别人博客中摘抄的:
简介:
其实doEnvents很简单,就是暂停一下当前模块Code,好让你程序可以响应其它事件、消息……
响应完其它事之后又回去继续执行刚才的Code (允许窗体在忙时响应 UI 输入)

生动描述:
程序对CPU说,哥们,你去干别的事情吧

好处或意义:
全部显示,处理当前在消息队列中的所有Windows消息.
在大量循环中使用可以有效避免假死机现象

注意:
调用该方法可以在某消息引发事件时导致重新输入代码.

额外了解:
Window窗体运行机制:
当运行Windows窗体时,它将创建新窗体,然后该窗体等待处理事件
该窗体在每次处理事件时,均将处理与该事件关联的所有代码。
所有其他事件在队列中等待。
在代码处理事件时,应用程序并不响应。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xochenlin/archive/2008/10/31/3187129.aspx

当我们在事件里面进行大量的循环操作时,windows将等待循环结束。此时界面是得不到响应的。为了在WinForm界面上实时地显示每次循环得到的结果,可以用.net提供的Application.DoEvents();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值