この記事は翻訳者によって翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。
訳文
原文
このトピックはまだ評価されていません - このトピックを評価する

Application.DoEvents メソッド

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

名前空間:  System.Windows.Forms
アセンブリ:  System.Windows.Forms (System.Windows.Forms.dll 内)
public static void DoEvents()

Windows フォームを実行すると、新しいフォームが作成され、フォームはイベントが処理されるまで待機します。 フォームがイベントを処理するたびに、イベントに関連付けられたコードがすべて処理されます。 他のすべてのイベントはキューの中で待機します。 コードがイベントを処理する間、アプリケーションは応答しません。 たとえば、ウィンドウは別のウィンドウが最前面に移動された場合は再描画されません。

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

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

通常、このメソッドはループ内でメッセージを処理するために使います。

Caution メモ 注意

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

DoEvents メソッドを使用するコード例を次に示します。 このコードを実行すると、ユーザーは OpenFileDialog からグラフィックス ファイルを選択できます。 選択したファイルはフォームに表示されます。 DoEvents メソッドの働きにより、フォームは開かれるグラフィックス ファイルごとに再描画されます。 この例を実行するには、PictureBox1 という名前の PictureBoxOpenFileDialog1 という名前の OpenFileDialog、および fileButton という名前のボタンが配置されているフォームに次のコードを貼り付けます。 次に、フォームのコンストラクターまたは Load メソッドから、InitializePictureBox メソッドと InitializeOpenFileDialog メソッドを呼び出します。

メモ メモ

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

またこの例では、Button コントロールの Control.Click イベントおよび OpenFileDialogFileOk イベントが、この例で定義されているイベント ハンドラーに関連付けられている必要があります。 このコードの実行を開始したら、ボタンをクリックしてダイアログ ボックスを表示します。


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




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.  Call the InitializePictureBox and 
InitializeOpenFileDialog methods from the form's constructor or 
Load method. When the example is running, display the dialog by clicking
the button.    


.NET Framework

サポート対象: 4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

サポート対象: 4、3.5 SP1

Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2

.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
この情報は役に立ちましたか。
(残り 1500 文字)
コミュニティ コンテンツ 追加
注釈 FAQ