Application.DoEvents メソッド

定義

メッセージ キューに現在ある Windows メッセージをすべて処理します。

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

次のコード例では、このメソッドの使用方法を DoEvents 示します。 この例を実行すると、ユーザーは OpenFileDialog. 選択したファイルがフォームに表示されます。 このメソッドは DoEvents 、開かれた各グラフィックス ファイルのフォームを強制的に再描画します。 この例を実行するには、名前付き、名前OpenFileDialog1付きPictureBox1OpenFileDialogボタンを含むPictureBoxフォームに次のコードをfileButton貼り付けます。 フォームのInitializePictureBoxコンストラクターまたはLoadメソッドから and InitializeOpenFileDialog メソッドを呼び出します。

注意

Visual Studioでは、ドラッグ操作を使用してフォームに追加OpenFileDialogする場合は、新しいインスタンスを作成する行を削除して、次InitializeOpenFileDialogOpenFileDialogメソッドを変更する必要があります。

また、この例では、コントロールのButtonイベントとイベントOpenFileDialogFileOk、例で定義されているイベント ハンドラーに接続されている必要Control.Clickがあります。 この例を実行しているときに、ボタンをクリックしてダイアログ ボックスを表示します。

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 すと、アプリケーションは他のイベントを処理できます。 たとえば、データを追加してコードに追加DoEventsするListBoxフォームがある場合、別のウィンドウがドラッグされたときにフォームが再描画されます。 コードから削除 DoEvents した場合、ボタンのクリック イベント ハンドラーの実行が完了するまで、フォームは再描画されません。 メッセージングの詳細については、「Windows フォームのユーザー入力」を参照してください。

Visual Basic 6.0 とは異なり、DoEventsメソッドはメソッドをThread.Sleep呼び出しません。

通常は、このメソッドをループ内で使用してメッセージを処理します。

注意事項

このメソッドを呼び出すと、待機中のすべてのウィンドウ メッセージが処理されている間、現在のスレッドが中断されます。 メッセージによってイベントがトリガーされた場合、アプリケーション コードの他の領域が実行される可能性があります。 これにより、アプリケーションでデバッグが困難な予期しない動作が発生する可能性があります。 長い時間がかかる操作や計算を実行する場合は、多くの場合、新しいスレッドでこれらの操作を実行することをお勧めします。 非同期プログラミングの詳細については、「 非同期プログラミング モデル (APM)」を参照してください。

適用対象

こちらもご覧ください