Application.DoEvents Application.DoEvents Application.DoEvents Application.DoEvents Method

定義

處理目前在訊息佇列中的所有 Windows 訊息。Processes all Windows messages currently in the message queue.

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

範例

下列程式碼範例將示範如何DoEvents使用方法。The following code example demonstrates using the DoEvents method. 當範例執行時, 使用者可以從OpenFileDialog選取圖形檔案。When the example runs, a user can select graphics files from an OpenFileDialog. 選取的檔案會顯示在表單中。The selected files are displayed in the form. DoEvents方法會強制重新繪製每個開啟的圖形檔案的表單。The DoEvents method forces a repaint of the form for each graphics file opened. 若要執行此範例, 請將下列程式碼貼入包含PictureBox名為PictureBox1 OpenFileDialog的表單OpenFileDialog1中、名為的fileButton, 以及名為的按鈕。To run this example, paste the following code in a form containing a PictureBox named PictureBox1, an OpenFileDialog named OpenFileDialog1, and a button named fileButton. 從表單InitializeOpenFileDialog的「函式」或Load 「方法」呼叫和方法。InitializePictureBoxCall the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method.

注意

在 Visual Studio 中, 如果您使用OpenFileDialog拖曳作業將加入至您的表單, 就必須移除建立新實例InitializeOpenFileDialogOpenFileDialog程式程式碼, 以修改下列方法。In Visual Studio, if you add an OpenFileDialog to your form by using a drag operation, you will have to modify the following InitializeOpenFileDialog method by removing the line that creates a new instance of OpenFileDialog.

Control.Click此範例也要求Button控制項的事件OpenFileDialogFileOk的事件會連接到範例中定義的事件處理常式。The example also requires that the Control.Click event of the Button control and the FileOk event of the OpenFileDialog are connected to the event handlers defined in the example. 當範例執行時, 按一下按鈕即可顯示對話方塊。When the example is running, display the dialog box by clicking the button.

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 時, 它會建立新的表單, 然後等候事件處理。When you run a Windows Form, it creates the new form, which then waits for events to handle. 每次表單處理事件時, 它會處理與該事件相關聯的所有程式碼。Each time the form handles an event, it processes all the code associated with that event. 所有其他事件都會在佇列中等候。All other events wait in the queue. 當您的程式碼處理事件時, 您的應用程式不會回應。While your code handles the event, your application does not respond. 例如, 如果將另一個視窗拖曳到上方, 視窗就不會重新繪製。For example, the window does not repaint if another window is dragged on top.

如果您在DoEvents程式碼中呼叫, 您的應用程式可以處理其他事件。If you call DoEvents in your code, your application can handle the other events. 例如, 如果您的表單會將資料新增至ListBox , 並將加入DoEvents至您的程式碼, 您的表單會在將另一個視窗拖曳至其上時重新繪製。For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it. 如果您從DoEvents程式碼中移除, 則在按鈕的 click 事件處理常式完成執行之前, 您的表單不會重新繪製。If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing. 如需訊息的詳細資訊, 請參閱Windows Forms 中的使用者輸入For more information on messaging, see User Input in Windows Forms.

不同于DoEvents Visual Basic 6.0, 方法不會Thread.Sleep呼叫方法。Unlike Visual Basic 6.0, the DoEvents method does not call the Thread.Sleep method.

一般來說, 您會在迴圈中使用這個方法來處理訊息。Typically, you use this method in a loop to process messages.

警告

呼叫這個方法會在處理所有等候中的視窗訊息時暫停目前的執行緒。Calling this method causes the current thread to be suspended while all waiting window messages are processed. 如果訊息造成觸發事件, 則應用程式程式碼的其他部分可能會執行。If a message causes an event to be triggered, then other areas of your application code may execute. 這可能會導致您的應用程式呈現不容易進行 debug 的非預期行為。This can cause your application to exhibit unexpected behaviors that are difficult to debug. 如果您執行的作業或計算需要很長的時間, 通常最好是在新的執行緒上執行這些作業。If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. 如需非同步程式設計的詳細資訊, 請參閱非同步程式設計模型 (APM)For more information about asynchronous programming, see Asynchronous Programming Model (APM).

適用於

另請參閱