Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Application.DoEvents-Methode: ()

 

Veröffentlicht: Oktober 2016

Verarbeitet alle Windows-Meldungen, die sich derzeit in der Meldungswarteschlange befinden.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public static void DoEvents()

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.

If you call M:System.Windows.Forms.Application.DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a T:System.Windows.Forms.ListBox and add M:System.Windows.Forms.Application.DoEvents to your code, your form repaints when another window is dragged over it. If you remove M:System.Windows.Forms.Application.DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing. For more information on messaging, see User Input in Windows Forms.

Unlike Visual Basic 6.0, the M:System.Windows.Forms.Application.DoEvents method does not call the M:System.Threading.Thread.Sleep(System.Int32) method.

Typically, you use this method in a loop to process messages.

System_CAPS_cautionAchtung

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. 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. For more information about asynchronous programming, see Asynchronous Programming Model (APM).

The following code example demonstrates using the M:System.Windows.Forms.Application.DoEvents method. When the example runs, a user can select graphics files from an T:System.Windows.Forms.OpenFileDialog. The selected files are displayed in the form. The M:System.Windows.Forms.Application.DoEvents method forces a repaint of the form for each graphics file opened. To run this example, paste the following code in a form containing a T:System.Windows.Forms.PictureBox named PictureBox1, an T:System.Windows.Forms.OpenFileDialog named OpenFileDialog1, and a button named fileButton. Call the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method.

System_CAPS_noteHinweis

In vsprvs, if you add an T:System.Windows.Forms.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 T:System.Windows.Forms.OpenFileDialog.

The example also requires that the E:System.Windows.Forms.Control.Click event of the T:System.Windows.Forms.Button control and the E:System.Windows.Forms.FileDialog.FileOk event of the T:System.Windows.Forms.OpenFileDialog are connected to the event handlers defined in the example. When the example is running, display the dialog box by clicking the button.

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

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: