Application.DoEvents 메서드

정의

현재 메시지 큐에 있는 모든 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. InitializePictureBox InitializeOpenFileDialog 폼의 생성자 또는 메서드에서 및 메서드를 호출 Load 합니다.Call the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method.

참고

Visual Studio에서 OpenFileDialog 끌기 작업을 사용 하 여 폼에를 추가 하는 경우 InitializeOpenFileDialog 의 새 인스턴스를 만드는 줄을 제거 하 여 다음 메서드를 수정 해야 합니다 OpenFileDialog .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 컨트롤의 이벤트 ButtonFileOk 의 이벤트가 OpenFileDialog 예제에 정의 된 이벤트 처리기에 연결 되어야 합니다.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.

Visual Basic 6.0와 달리 DoEvents 메서드는 메서드를 호출 하지 않습니다 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. 이 인해 애플리케이션의 디버그 하기 어려운 예기치 않은 동작이 발생할 수 있습니다.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).

적용 대상

추가 정보