Application.DoEvents 方法

定義

處理目前在訊息佇列中的所有 Windows 訊息。

public:
 static void DoEvents();
public static void DoEvents ();
static member DoEvents : unit -> unit
Public Shared Sub DoEvents ()

範例

下列程式碼範例示範如何使用 DoEvents 方法。 執行範例時,使用者可以從 OpenFileDialog 中選取圖形檔案。 選取的檔案會顯示在表單中。 方法 DoEvents 會強制開啟每個圖形檔案的表單重繪。 若要執行此範例,請將下列程式碼貼到表單中, PictureBox 其中包含名為 的 、 OpenFileDialog 名為 PictureBox1OpenFileDialog1 的 ,以及名為 的 fileButton 按鈕。 InitializePictureBox從表單的建構函式或 Load 方法呼叫 和 InitializeOpenFileDialog 方法。

注意

在 Visual Studio 中,如果您使用拖曳作業將 新增 OpenFileDialog 至表單,則必須移除建立 新實例的 OpenFileDialog 行來修改下列 InitializeOpenFileDialog 方法。

此範例也需要 Control.Click 控制項的 Button 事件和 FileOkOpenFileDialog 事件連接到範例中定義的事件處理常式。 執行範例時,按一下按鈕來顯示對話方塊。

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.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 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

備註

當您執行 Windows Form 時,它會建立新的表單,然後等候事件進行處理。 每次表單處理事件時,都會處理與該事件相關聯的所有程式碼。 所有其他事件都會在佇列中等候。 雖然您的程式碼會處理事件,但您的應用程式不會回應。 例如,如果另一個視窗拖曳在頂端,則視窗不會重新繪製。

如果您在程式碼中呼叫 DoEvents ,您的應用程式可以處理其他事件。 例如,如果您有一個將資料新增至 的表單,並新增 DoEventsListBox 您的程式碼,則當另一個視窗拖曳到表單上方時,您的表單會重新繪製。 如果您從程式碼中移除 DoEvents ,在按鈕的 click 事件處理常式執行完成之前,您的表單將不會重新繪製。 如需傳訊的詳細資訊,請參閱Windows Forms 中的使用者輸入

不同于 Visual Basic 6.0, DoEvents 方法不會呼叫 Thread.Sleep 方法。

一般而言,您會在迴圈中使用此方法來處理訊息。

警告

呼叫這個方法會導致處理所有等候視窗訊息時暫停目前的執行緒。 如果訊息導致觸發事件,則應用程式程式碼的其他區域可能會執行。 這可能會導致您的應用程式呈現難以偵錯的非預期行為。 如果您執行需要很長時間的作業或計算,最好是在新的執行緒上執行這些作業。 如需非同步程式設計的詳細資訊,請參閱 非同步程式設計模型 (APM)

適用於

另請參閱