更新 : 2007 年 11 月
コントロールの基になるウィンドウ ハンドルを所有するスレッド上で、指定したデリゲートを実行します。
名前空間 :
System.Windows.Forms アセンブリ :
System.Windows.Forms (System.Windows.Forms.dll 内)
Public Function Invoke ( _
method As Delegate _
) As Object
Dim instance As Control
Dim method As [Delegate]
Dim returnValue As Object
returnValue = instance.Invoke(method)
public Object Invoke(
Delegate method
)
public:
Object^ Invoke(
Delegate^ method
)
public Object Invoke(
Delegate method
)
public function Invoke(
method : Delegate
) : Object
戻り値
型 :
System..::.Object
呼び出されているデリゲートからの戻り値。デリゲートに戻り値がない場合は nullNothingnullptrnull 参照 (Visual Basic では Nothing)。
デリゲートは、C または C++ 言語の関数ポインタに似ています。デリゲートは、デリゲート オブジェクト内でメソッドへの参照をカプセル化します。デリゲート オブジェクトは、参照されるメソッドを呼び出すコードに渡されるため、呼び出されるメソッドは、コンパイル時には不明である可能性があります。C または C++ の関数ポインタと異なり、デリゲートはオブジェクト指向かつタイプ セーフであり、より安全です。
Invoke メソッドは、現在のコントロールの基になるウィンドウ ハンドルがまだ存在しない場合、ウィンドウ ハンドルを持つコントロールまたはフォームが見つかるまでコントロールの親チェインを検索します。適切なハンドルが見つからない場合、Invoke メソッドは例外をスローします。呼び出し中に発生する例外は、呼び出し元まで反映されます。
デリゲートは、EventHandler のインスタンスである場合もあります。その場合は、送信元のパラメータにこのコントロールが含まれ、イベント パラメータには EventArgs..::.Empty が含まれます。デリゲートは MethodInvoker のインスタンス、または空のパラメータ リストをとるその他のデリゲートである場合もあります。EventHandler デリゲートまたは MethodInvoker デリゲートの呼び出しの方が、別の種類のデリゲートの呼び出しよりも高速に実行されます。
メモ : |
|---|
メッセージを処理するスレッドが既に非アクティブになっていた場合は、例外がスローされます。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォーム メモ :
.NET Compact Framework アプリケーションでは、デリゲートは EventHandler のインスタンスである必要があります。例については、デリゲートのサンプル のトピックを参照してください。
デリゲートを含むコントロールを表示するコード例を次に示します。デリゲートは、項目をリスト ボックスに追加するメソッドをカプセル化します。このメソッドは、フォームの基になるハンドルを所有するスレッドで実行されます。ユーザーがボタンをクリックすると、Invoke はデリゲートを実行します。
' 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
// 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);
}
}
// 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
参照
その他の技術情報