Application.DoEvents 方法

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Shared Sub DoEvents
用法

Application.DoEvents
public static void DoEvents ()
public:
static void DoEvents ()
public static void DoEvents ()
public static function DoEvents ()

备注

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

如果在代码中调用 DoEvents,则您的应用程序可以处理其他事件。例如,如果您有向 ListBox 添加数据的窗体,并将 DoEvents 添加到代码中,那么当将另一窗口拖到您的窗体上时,该窗体将重新绘制。如果从代码中移除 DoEvents,那么在按钮的单击事件处理程序执行结束以前,您的窗体不会重新绘制。

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

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

警告

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

示例

下面的代码示例阐释了如何使用 DoEvents 方法。当该示例运行时,用户可以从 OpenFileDialog 中选择图形文件。选定的文件显示在窗体中。DoEvents 方法强制对每个打开的图形文件的窗体进行重新绘制。若要运行此示例,请将下面的代码粘贴到一个窗体中,该窗体包含一个名为 PictureBox1PictureBox、一个名为 OpenFileDialog1OpenFileDialog 以及一个名为 fileButton 的按钮。从窗体的构造函数或 Load 方法调用 InitializePictureBoxInitializeOpenFileDialog 方法。该示例还要求将 Button 控件的 Control.Click 事件和 OpenFileDialogFileOk 事件连接到该示例中定义的事件处理程序。当该示例运行时,请通过单击按钮显示对话框。

Private Sub InitializePictureBox()
    Me.PictureBox1 = New System.Windows.Forms.PictureBox
    Me.PictureBox1.BorderStyle = _
        System.Windows.Forms.BorderStyle.FixedSingle
    Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    Me.PictureBox1.Location = New System.Drawing.Point(72, 112)
    Me.PictureBox1.Name = "PictureBox1"
    Me.PictureBox1.Size = New System.Drawing.Size(160, 136)
    Me.PictureBox1.TabStop = False
End Sub

Private Sub InitializeOpenFileDialog()
    Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

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

    ' Allow the user to select multiple images.
    Me.OpenFileDialog1.Multiselect = True
    Me.OpenFileDialog1.Title = "My Image Browser"
End Sub

Private Sub fileButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles FileButton.Click
    OpenFileDialog1.ShowDialog()
End Sub


' This method handles the FileOK event.  It opens each file 
' selected and loads the image from a stream into PictureBox1.
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
 Handles OpenFileDialog1.FileOk

    Me.Activate()
    Dim file, files() As 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.        
    For Each file In files
        Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(file)
        Dim fileStream As System.IO.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)
    Next
    PictureBox1.Image = Nothing
End Sub
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;
}
void InitializePictureBox()
{
   this->PictureBox1 = gcnew System::Windows::Forms::PictureBox;
   this->PictureBox1->BorderStyle =
      System::Windows::Forms::BorderStyle::FixedSingle;
   this->PictureBox1->SizeMode = PictureBoxSizeMode::StretchImage;
   this->PictureBox1->Location = System::Drawing::Point( 72, 112 );
   this->PictureBox1->Name = "PictureBox1";
   this->PictureBox1->Size = System::Drawing::Size( 160, 136 );
   this->PictureBox1->TabIndex = 6;
   this->PictureBox1->TabStop = false;
}

void InitializeOpenFileDialog()
{
   this->OpenFileDialog1 = gcnew 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";
}

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.
void OpenFileDialog1_FileOk( Object^ sender,
   System::ComponentModel::CancelEventArgs^ e )
{
   this->Activate();
   array<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.        
   for each ( String^ file in files )
   {
      System::IO::FileInfo^ fileInfo = gcnew 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 = nullptr;
}
private void InitializePictureBox()
{
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.pictureBox1.set_BorderStyle(
        System.Windows.Forms.BorderStyle.FixedSingle);
    this.pictureBox1.set_SizeMode(PictureBoxSizeMode.StretchImage);
    this.pictureBox1.set_Location(new System.Drawing.Point(72, 112));
    this.pictureBox1.set_Name("pictureBox1");
    this.pictureBox1.set_Size(new System.Drawing.Size(160, 136));
    this.pictureBox1.set_TabIndex(6);
    this.pictureBox1.set_TabStop(false);
} //InitializePictureBox

private void InitializeOpenFileDialog()
{
    this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
    // Set the file dialog to filter for graphics files.
    this.openFileDialog1.set_Filter("Images (*.BMP;*.JPG;*.GIF)|" 
        + "*.BMP;*.JPG;*.GIF|All files (*.*)|*.*");
    // Allow the user to select multiple images.
    this.openFileDialog1.set_Multiselect(true);
    this.openFileDialog1.set_Title("My Image Browser");
} //InitializeOpenFileDialog

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

// 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.get_FileNames();
    // Open each file and display the image in pictureBox1.
    // Call Application.DoEvents to force a repaint after each
    // file is read.
    for (int iCtr = 0; iCtr < files.length; iCtr++) {
        String file = files[iCtr];
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
        System.IO.FileStream fileStream = fileInfo.OpenRead();
        pictureBox1.set_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.set_Image(null);
} //openFileDialog1_FileOk

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Application 类
Application 成员
System.Windows.Forms 命名空间
Exit
ExitThread
Run