Application.DoEvents 方法

定义

处理当前在消息队列中的所有 Windows 消息。

C#
public static void DoEvents();

示例

下面的代码示例演示如何使用 DoEvents 方法。 运行示例时,用户可以从 OpenFileDialog中选择图形文件。 所选文件显示在窗体中。 方法 DoEvents 强制对打开的每个图形文件重新绘制窗体。 若要运行此示例,请将以下代码粘贴到包含PictureBox名为 、OpenFileDialog名为 PictureBox1OpenFileDialog1的 和名为 的按钮的fileButton窗体中。 InitializePictureBox从窗体的构造函数或 Load 方法调用 和 InitializeOpenFileDialog 方法。

备注

在 Visual Studio 中,如果使用拖动操作将 添加到OpenFileDialog窗体,则必须通过删除创建新 实例的OpenFileDialog行来修改以下InitializeOpenFileDialog方法。

该示例还要求 Control.Click 控件的 Button 事件和 FileOkOpenFileDialog 事件连接到示例中定义的事件处理程序。 当示例正在运行时,通过单击 按钮显示对话框。

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

注解

运行 Windows 窗体时,它会创建新窗体,然后等待事件处理。 每次窗体处理事件时,它都会处理与该事件关联的所有代码。 所有其他事件都在队列中等待。 当代码处理事件时,应用程序不会响应。 例如,如果将另一个窗口拖到顶部,则窗口不会重新绘制。

如果在代码中调用 DoEvents ,应用程序可以处理其他事件。 例如,如果有一个向 中添加数据 ListBox 并添加到 DoEvents 代码的窗体,则在将另一个窗口拖到该窗体上时,该窗体将重新绘制。 如果从代码中删除 DoEvents ,则在按钮的单击事件处理程序执行完毕之前,窗体不会重新绘制。 有关消息传递的详细信息,请参阅 Windows 窗体 中的用户输入

与 Visual Basic 6.0 不同 DoEvents , 方法不调用 Thread.Sleep 方法。

通常,在循环中使用此方法来处理消息。

注意

调用此方法会导致在处理所有等待窗口消息时挂起当前线程。 如果消息导致触发事件,则应用程序代码的其他区域可能会执行。 这可能会导致应用程序表现出难以调试的意外行为。 如果执行需要很长时间的操作或计算,通常最好在新线程上执行这些操作。 有关异步编程的详细信息,请参阅 异步编程模型 (APM)

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅