.NET Framework クラス ライブラリ
Control..::.Invoke メソッド (Delegate)

更新 : 2007 年 11 月

コントロールの基になるウィンドウ ハンドルを所有するスレッド上で、指定したデリゲートを実行します。

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

構文

Visual Basic (宣言)
Public Function Invoke ( _
    method As Delegate _
) As Object
Visual Basic (使用法)
Dim instance As Control
Dim method As [Delegate]
Dim returnValue As Object

returnValue = instance.Invoke(method)
C#
public Object Invoke(
    Delegate method
)
Visual C++
public:
Object^ Invoke(
    Delegate^ method
)
J#
public Object Invoke(
    Delegate method
)
JScript
public function Invoke(
    method : Delegate
) : Object

パラメータ

method
型 : System..::.Delegate

コントロールのスレッド コンテキストで呼び出されるメソッドを格納しているデリゲート。

戻り値

型 : System..::.Object

呼び出されているデリゲートからの戻り値。デリゲートに戻り値がない場合は nullNothingnullptrnull 参照 (Visual Basic では Nothing)

解説

デリゲートは、C または C++ 言語の関数ポインタに似ています。デリゲートは、デリゲート オブジェクト内でメソッドへの参照をカプセル化します。デリゲート オブジェクトは、参照されるメソッドを呼び出すコードに渡されるため、呼び出されるメソッドは、コンパイル時には不明である可能性があります。C または C++ の関数ポインタと異なり、デリゲートはオブジェクト指向かつタイプ セーフであり、より安全です。

Invoke メソッドは、現在のコントロールの基になるウィンドウ ハンドルがまだ存在しない場合、ウィンドウ ハンドルを持つコントロールまたはフォームが見つかるまでコントロールの親チェインを検索します。適切なハンドルが見つからない場合、Invoke メソッドは例外をスローします。呼び出し中に発生する例外は、呼び出し元まで反映されます。

メモ :

コントロールのハンドルが既に作成されている場合、InvokeRequired プロパティ以外に、スレッド セーフである 4 つのメソッド InvokeBeginInvokeEndInvoke、および CreateGraphics がコントロールにあります。コントロールのハンドルがバックグラウンド スレッドで作成される前に CreateGraphics を呼び出すと、無効なスレッド間の呼び出しが発生する可能性があります。その他のすべてのメソッドの呼び出しについては、Invoke メソッドの 1 つを使用して、その呼び出しをコントロールのスレッドにマーシャリングする必要があります。

デリゲートは、EventHandler のインスタンスである場合もあります。その場合は、送信元のパラメータにこのコントロールが含まれ、イベント パラメータには EventArgs..::.Empty が含まれます。デリゲートは MethodInvoker のインスタンス、または空のパラメータ リストをとるその他のデリゲートである場合もあります。EventHandler デリゲートまたは MethodInvoker デリゲートの呼び出しの方が、別の種類のデリゲートの呼び出しよりも高速に実行されます。

メモ :

メッセージを処理するスレッドが既に非アクティブになっていた場合は、例外がスローされます。

Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォーム メモ :

.NET Compact Framework アプリケーションでは、デリゲートは EventHandler のインスタンスである必要があります。例については、デリゲートのサンプル のトピックを参照してください。


デリゲートを含むコントロールを表示するコード例を次に示します。デリゲートは、項目をリスト ボックスに追加するメソッドをカプセル化します。このメソッドは、フォームの基になるハンドルを所有するスレッドで実行されます。ユーザーがボタンをクリックすると、Invoke はデリゲートを実行します。

Visual Basic
' The following code assumes a 'ListBox' and a 'Button' control are added to a form, 
' containing a delegate which encapsulates a method that adds items to the listbox.
Public Class MyThreadClass
   Private myFormControl1 As MyFormControl

   Public Sub New(myForm As MyFormControl)
      myFormControl1 = myForm
   End Sub 'New

   Public Sub Run()
      ' Execute the specified delegate on the thread that owns
      ' 'myFormControl1' control's underlying window handle.
      myFormControl1.Invoke(myFormControl1.myDelegate)
   End Sub 'Run

End Class 'MyThreadClass
C#
// The following code assumes a 'ListBox' and a 'Button' control are added to a form, 
// containing a delegate which encapsulates a method that adds items to the listbox.

   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }

      public void Run()
      {
         // Execute the specified delegate on the thread that owns
         // 'myFormControl1' control's underlying window handle.
         myFormControl1.Invoke(myFormControl1.myDelegate);
      }
   }
Visual C++
// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// containing a delegate which encapsulates a method that adds items to the listbox.
public ref class MyThreadClass
{
private:
   MyFormControl^ myFormControl1;

public:
   MyThreadClass( MyFormControl^ myForm )
   {
      myFormControl1 = myForm;
   }

   void Run()
   {
      // Execute the specified delegate on the thread that owns
      // 'myFormControl1' control's underlying window handle.
      myFormControl1->Invoke( myFormControl1->myDelegate );
   }
};
プラットフォーム

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

バージョン情報

.NET Framework

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

.NET Compact Framework

サポート対象 : 3.5、2.0、1.0
参照

参照

その他の技術情報

タグ :


Page view tracker