Application.DoEvents 메서드

정의

현재 메시지 큐에 있는 모든 Windows 메시지를 처리합니다.

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

예제

다음 코드 예제에서는 메서드를 사용하는 방법을 DoEvents 보여 줍니다. 예제가 실행되면 사용자는 에서 그래픽 파일을 OpenFileDialog선택할 수 있습니다. 선택한 파일이 양식에 표시됩니다. 이 메서드는 DoEvents 열려 있는 각 그래픽 파일에 대해 폼을 다시 칠합니다. 이 예제를 실행하려면 이름, 명명 PictureBoxPictureBox1 된 이름 OpenFileDialog및 단추OpenFileDialog1가 포함된 fileButton 폼에 다음 코드를 붙여넣습니다. 폼의 InitializePictureBox 생성자 또는 InitializeOpenFileDialog 메서드에서 메서드 및 Load 메서드를 호출합니다.

메모

Visual Studio 끌어서 폼에 OpenFileDialog 추가하는 경우 InitializeOpenFileDialog 새 인스턴스를 만드는 줄을 제거하여 다음 OpenFileDialog 메서드를 수정해야 합니다.

또한 이 예제에서는 Control.Click 컨트롤의 Button 이벤트와 FileOk 해당 이벤트가 OpenFileDialog 예제에 정의된 이벤트 처리기에 연결되어야 합니다. 예제가 실행 중이면 단추를 클릭하여 대화 상자를 표시합니다.

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 폼을 실행하면 새 폼이 만들어지며, 그러면 이벤트가 처리되기를 기다립니다. 폼이 이벤트를 처리할 때마다 해당 이벤트와 연결된 모든 코드를 처리합니다. 다른 모든 이벤트는 큐에서 대기합니다. 코드가 이벤트를 처리하는 동안 애플리케이션은 응답하지 않습니다. 예를 들어 다른 창을 맨 위로 끌면 창이 다시 그려지지 않습니다.

코드에서 호출 DoEvents 하는 경우 애플리케이션에서 다른 이벤트를 처리할 수 있습니다. 예를 들어 데이터를 추가하고 코드에 ListBox 추가하는 DoEvents 폼이 있는 경우 다른 창을 끌어다 놓고 폼이 다시 그려집니다. 코드에서 제거 DoEvents 하면 단추의 클릭 이벤트 처리기 실행이 완료될 때까지 양식이 다시 그려지지 않습니다. 메시징에 대한 자세한 내용은 Windows Forms 참조하세요.

Visual Basic 6.0과 달리 DoEvents 메서드는 Thread.Sleep 메서드를 호출하지 않습니다.

일반적으로 루프에서 이 메서드를 사용하여 메시지를 처리합니다.

Caution

이 메서드를 호출하면 대기 중인 모든 창 메시지가 처리되는 동안 현재 스레드가 일시 중단됩니다. 메시지가 이벤트를 트리거하는 경우 애플리케이션 코드의 다른 영역이 실행될 수 있습니다. 이로 인해 애플리케이션에서 디버그하기 어려운 예기치 않은 동작이 표시될 수 있습니다. 시간이 오래 걸리는 작업 또는 계산을 수행하는 경우 새 스레드에서 이러한 작업을 수행하는 것이 좋습니다. 비동기 프로그래밍에 대한 자세한 내용은 APM(비동기 프로그래밍 모델)을 참조하세요.

적용 대상

추가 정보