Control.Invoke 메서드

정의

컨트롤의 내부 창 핸들이 있는 스레드에서 대리자를 실행합니다.

오버로드

Invoke(Action)

컨트롤의 내부 창 핸들이 있는 스레드에서 지정된 대리자를 실행합니다.

Invoke(Delegate)

컨트롤의 내부 창 핸들이 있는 스레드에서 지정된 대리자를 실행합니다.

Invoke(Delegate, Object[])

컨트롤의 내부 창 핸들이 있는 스레드에서 특정 인수 목록을 사용하여 지정된 대리자를 실행합니다.

Invoke<T>(Func<T>)

컨트롤의 내부 창 핸들이 있는 스레드에서 지정된 대리자를 실행합니다.

Invoke(Action)

컨트롤의 내부 창 핸들이 있는 스레드에서 지정된 대리자를 실행합니다.

public:
 void Invoke(Action ^ method);
public void Invoke (Action method);
member this.Invoke : Action -> unit
Public Sub Invoke (method As Action)

매개 변수

method
Action

컨트롤의 스레드 컨텍스트에서 호출될 메서드가 있는 대리자입니다.

적용 대상

Invoke(Delegate)

컨트롤의 내부 창 핸들이 있는 스레드에서 지정된 대리자를 실행합니다.

public:
 System::Object ^ Invoke(Delegate ^ method);
public object Invoke (Delegate method);
member this.Invoke : Delegate -> obj
Public Function Invoke (method As Delegate) As Object

매개 변수

method
Delegate

컨트롤의 스레드 컨텍스트에서 호출될 메서드가 있는 대리자입니다.

반환

호출되는 대리자의 반환 값이거나, 대리자에 반환 값이 없으면 null입니다.

예제

다음 코드 예제에서는 대리자를 포함 하는 컨트롤을 보여 줍니다. 대리자는 목록 상자에 항목을 추가하는 메서드를 캡슐화하고 이 메서드는 양식의 기본 핸들을 소유하는 스레드에서 실행됩니다. 사용자가 단추를 Invoke 클릭하면 대리자를 실행합니다.

/*
The following example demonstrates the 'Invoke(Delegate*)' method of 'Control class.
A 'ListBox' and a 'Button' control are added to a form, containing a delegate
which encapsulates a method that adds items to the listbox. This function is executed
on the thread that owns the underlying handle of the form. When user clicks on button
the above delegate is executed using 'Invoke' method.
*/

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Threading;

public ref class MyFormControl: public Form
{
public:
   delegate void AddListItem();
   AddListItem^ myDelegate;

private:
   Button^ myButton;
   Thread^ myThread;
   ListBox^ myListBox;

public:
   MyFormControl();
   void AddListItemMethod()
   {
      String^ myItem;
      for ( int i = 1; i < 6; i++ )
      {
         myItem = "MyListItem {0}",i;
         myListBox->Items->Add( myItem );
         myListBox->Update();
         Thread::Sleep( 300 );
      }
   }

private:
   void Button_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      myThread = gcnew Thread( gcnew ThreadStart( this, &MyFormControl::ThreadFunction ) );
      myThread->Start();
   }

   void ThreadFunction();
};


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


MyFormControl::MyFormControl()
{
   myButton = gcnew Button;
   myListBox = gcnew ListBox;
   myButton->Location = Point( 72, 160 );
   myButton->Size = System::Drawing::Size( 152, 32 );
   myButton->TabIndex = 1;
   myButton->Text = "Add items in list box";
   myButton->Click += gcnew EventHandler( this, &MyFormControl::Button_Click );
   myListBox->Location = Point( 48, 32 );
   myListBox->Name = "myListBox";
   myListBox->Size = System::Drawing::Size( 200, 95 );
   myListBox->TabIndex = 2;
   ClientSize = System::Drawing::Size( 292, 273 );
   array<Control^>^ temp0 = {myListBox,myButton};
   Controls->AddRange( temp0 );
   Text = " 'Control_Invoke' example";
   myDelegate = gcnew AddListItem( this, &MyFormControl::AddListItemMethod );
}

void MyFormControl::ThreadFunction()
{
   MyThreadClass^ myThreadClassObject = gcnew MyThreadClass( this );
   myThreadClassObject->Run();
}

int main()
{
   MyFormControl^ myForm = gcnew MyFormControl;
   myForm->ShowDialog();
}
/*
The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.
A 'ListBox' and a 'Button' control are added to a form, containing a delegate
which encapsulates a method that adds items to the listbox. This function is executed
on the thread that owns the underlying handle of the form. When user clicks on button
the above delegate is executed using 'Invoke' method.


*/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

   public class MyFormControl : Form
   {
      public delegate void AddListItem();
      public AddListItem myDelegate;
      private Button myButton;
      private Thread myThread;
      private ListBox myListBox;
      public MyFormControl()
      {
         myButton = new Button();
         myListBox = new ListBox();
         myButton.Location = new Point(72, 160);
         myButton.Size = new Size(152, 32);
         myButton.TabIndex = 1;
         myButton.Text = "Add items in list box";
         myButton.Click += new EventHandler(Button_Click);
         myListBox.Location = new Point(48, 32);
         myListBox.Name = "myListBox";
         myListBox.Size = new Size(200, 95);
         myListBox.TabIndex = 2;
         ClientSize = new Size(292, 273);
         Controls.AddRange(new Control[] {myListBox,myButton});
         Text = " 'Control_Invoke' example";
         myDelegate = new AddListItem(AddListItemMethod);
      }
      static void Main()
      {
         MyFormControl myForm = new MyFormControl();
         myForm.ShowDialog();
      }
      public void AddListItemMethod()
      {
         String myItem;
         for(int i=1;i<6;i++)
         {
            myItem = "MyListItem" + i.ToString();
            myListBox.Items.Add(myItem);
            myListBox.Update();
            Thread.Sleep(300);
         }
      }
      private void Button_Click(object sender, EventArgs e)
      {
         myThread = new Thread(new ThreadStart(ThreadFunction));
         myThread.Start();
      }
      private void ThreadFunction()
      {
         MyThreadClass myThreadClassObject  = new MyThreadClass(this);
         myThreadClassObject.Run();
      }
   }

// 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 example demonstrates the 'Invoke(Delegate)' method of 'Control class.
' A 'ListBox' and a 'Button' control are added to a form, containing a delegate
' which encapsulates a method that adds items to the listbox. This function is executed
' on the thread that owns the underlying handle of the form. When user clicks on button
' the above delegate is executed using 'Invoke' method.

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class MyFormControl
   Inherits Form

   Delegate Sub AddListItem()
   Public myDelegate As AddListItem
   Private myButton As Button
   Private myThread As Thread
   Private myListBox As ListBox

   Public Sub New()
      myButton = New Button()
      myListBox = New ListBox()
      myButton.Location = New Point(72, 160)
      myButton.Size = New Size(152, 32)
      myButton.TabIndex = 1
      myButton.Text = "Add items in list box"
      AddHandler myButton.Click, AddressOf Button_Click
      myListBox.Location = New Point(48, 32)
      myListBox.Name = "myListBox"
      myListBox.Size = New Size(200, 95)
      myListBox.TabIndex = 2
      ClientSize = New Size(292, 273)
      Controls.AddRange(New Control() {myListBox, myButton})
      Text = " 'Control_Invoke' example"
      myDelegate = New AddListItem(AddressOf AddListItemMethod)
   End Sub

   Shared Sub Main()
      Dim myForm As New MyFormControl()
      myForm.ShowDialog()
   End Sub

   Public Sub AddListItemMethod()
      Dim myItem As String
      Dim i As Integer
      For i = 1 To 5
         myItem = "MyListItem" + i.ToString()
         myListBox.Items.Add(myItem)
         myListBox.Update()
         Thread.Sleep(300)
      Next i
   End Sub

   Private Sub Button_Click(sender As Object, e As EventArgs)
      myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
      myThread.Start()
   End Sub

   Private Sub ThreadFunction()
      Dim myThreadClassObject As New MyThreadClass(Me)
      myThreadClassObject.Run()
   End Sub
End Class


' 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

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

End Class

설명

대리자는 C 또는 C++ 언어의 함수 포인터와 비슷합니다. 대리자는 대리자 개체 내의 메서드에 대한 참조를 캡슐화합니다. 그런 다음 참조된 메서드를 호출하는 코드에 대리자 개체를 전달할 수 있으며 컴파일 시 호출할 메서드를 알 수 없습니다. C 또는 C++의 함수 포인터와 달리 대리자는 개체 지향적이고 형식이 안전하며 더 안전합니다.

메서드는 Invoke 현재 컨트롤의 기본 창 핸들이 아직 없는 경우 창 핸들이 있는 컨트롤 또는 폼을 발견할 때까지 컨트롤의 부모 체인을 검색합니다. 적절한 핸들을 찾을 수 없는 경우 메서드는 Invoke 예외를 throw합니다. 호출 중에 발생하는 예외는 호출자에게 다시 전파됩니다.

참고

속성 외에도 InvokeRequired 스레드로부터 안전한 Invoke컨트롤에는 , , BeginInvokeEndInvokeCreateGraphics 및 컨트롤에 대한 핸들이 이미 만들어진 경우 네 가지 메서드가 있습니다. 백그라운드 스레드에서 컨트롤의 핸들을 만들기 전에 를 호출 CreateGraphics 하면 잘못된 스레드 간 호출이 발생할 수 있습니다. 다른 모든 메서드 호출의 경우 호출 메서드 중 하나를 사용하여 컨트롤의 스레드에 대한 호출을 마샬링해야 합니다.

대리자는 의 EventHandlerinstance 수 있습니다. 이 경우 보낸 사람 매개 변수에는 이 컨트롤이 포함되고 이벤트 매개 변수에는 가 포함EventArgs.Empty됩니다. 대리자는 의 instance MethodInvoker또는 void 매개 변수 목록을 사용하는 다른 대리자일 수도 있습니다. 또는 MethodInvoker 대리자를 EventHandler 호출하면 다른 유형의 대리자를 호출하는 것보다 빠릅니다.

참고

메시지를 처리해야 하는 스레드가 더 이상 활성 상태가 아니면 예외가 throw될 수 있습니다.

추가 정보

적용 대상

Invoke(Delegate, Object[])

컨트롤의 내부 창 핸들이 있는 스레드에서 특정 인수 목록을 사용하여 지정된 대리자를 실행합니다.

public:
 virtual System::Object ^ Invoke(Delegate ^ method, cli::array <System::Object ^> ^ args);
public:
 virtual System::Object ^ Invoke(Delegate ^ method, ... cli::array <System::Object ^> ^ args);
public object Invoke (Delegate method, object[] args);
public object Invoke (Delegate method, params object[] args);
public object Invoke (Delegate method, params object?[]? args);
abstract member Invoke : Delegate * obj[] -> obj
override this.Invoke : Delegate * obj[] -> obj
Public Function Invoke (method As Delegate, args As Object()) As Object
Public Function Invoke (method As Delegate, ParamArray args As Object()) As Object

매개 변수

method
Delegate

args 매개 변수에 있는 매개 변수의 수 및 형식과 동일한 매개 변수를 갖는 메서드의 대리자입니다.

args
Object[]

지정된 메서드에 인수로 전달하는 개체의 배열입니다. 메서드가 인수를 사용하지 않으면 이 매개 변수는 null이 될 수 있습니다.

반환

호출되는 대리자의 반환 값이 있는 Object이거나, 대리자에 반환 값이 없으면 null입니다.

구현

예제

다음 코드 예제에서는 대리자를 포함 하는 컨트롤을 보여 줍니다. 대리자는 목록 상자에 항목을 추가하는 메서드를 캡슐화하며, 이 메서드는 지정된 인수를 사용하여 양식의 기본 핸들을 소유하는 스레드에서 실행됩니다. 사용자가 단추를 Invoke 클릭하면 대리자를 실행합니다.

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Threading;
ref class MyFormControl: public Form
{
public:
   delegate void AddListItem( String^ myString );
   AddListItem^ myDelegate;

private:
   Button^ myButton;
   Thread^ myThread;
   ListBox^ myListBox;

public:
   MyFormControl();
   void AddListItemMethod( String^ myString );

private:
   void Button_Click( Object^ sender, EventArgs^ e );
   void ThreadFunction();
};

ref class MyThreadClass
{
private:
   MyFormControl^ myFormControl1;

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

   String^ myString;
   void Run()
   {
      for ( int i = 1; i <= 5; i++ )
      {
         myString = String::Concat( "Step number ", i, " executed" );
         Thread::Sleep( 400 );
         
         // Execute the specified delegate on the thread that owns
         // 'myFormControl1' control's underlying window handle with
         // the specified list of arguments.
         array<Object^>^myStringArray = {myString};
         myFormControl1->Invoke( myFormControl1->myDelegate, myStringArray );

      }
   }

};

MyFormControl::MyFormControl()
{
   myButton = gcnew Button;
   myListBox = gcnew ListBox;
   myButton->Location = Point(72,160);
   myButton->Size = System::Drawing::Size( 152, 32 );
   myButton->TabIndex = 1;
   myButton->Text = "Add items in list box";
   myButton->Click += gcnew EventHandler( this, &MyFormControl::Button_Click );
   myListBox->Location = Point(48,32);
   myListBox->Name = "myListBox";
   myListBox->Size = System::Drawing::Size( 200, 95 );
   myListBox->TabIndex = 2;
   ClientSize = System::Drawing::Size( 292, 273 );
   array<Control^>^formControls = {myListBox,myButton};
   Controls->AddRange( formControls );
   Text = " 'Control_Invoke' example ";
   myDelegate = gcnew AddListItem( this, &MyFormControl::AddListItemMethod );
}

void MyFormControl::AddListItemMethod( String^ myString )
{
   myListBox->Items->Add( myString );
}

void MyFormControl::Button_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   myThread = gcnew Thread( gcnew ThreadStart( this, &MyFormControl::ThreadFunction ) );
   myThread->Start();
}

void MyFormControl::ThreadFunction()
{
   MyThreadClass^ myThreadClassObject = gcnew MyThreadClass( this );
   myThreadClassObject->Run();
}

int main()
{
   MyFormControl^ myForm = gcnew MyFormControl;
   myForm->ShowDialog();
}
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

   public class MyFormControl : Form
   {
      public delegate void AddListItem(String myString);
      public AddListItem myDelegate;
      private Button myButton;
      private Thread myThread;
      private ListBox myListBox;
      public MyFormControl()
      {
         myButton = new Button();
         myListBox = new ListBox();
         myButton.Location = new Point(72, 160);
         myButton.Size = new Size(152, 32);
         myButton.TabIndex = 1;
         myButton.Text = "Add items in list box";
         myButton.Click += new EventHandler(Button_Click);
         myListBox.Location = new Point(48, 32);
         myListBox.Name = "myListBox";
         myListBox.Size = new Size(200, 95);
         myListBox.TabIndex = 2;
         ClientSize = new Size(292, 273);
         Controls.AddRange(new Control[] {myListBox,myButton});
         Text = " 'Control_Invoke' example ";
         myDelegate = new AddListItem(AddListItemMethod);
      }
      static void Main()
      {
         MyFormControl myForm = new MyFormControl();
         myForm.ShowDialog();
      }
      public void AddListItemMethod(String myString)
      {
            myListBox.Items.Add(myString);
      }
      private void Button_Click(object sender, EventArgs e)
      {
         myThread = new Thread(new ThreadStart(ThreadFunction));
         myThread.Start();
      }
      private void ThreadFunction()
      {
         MyThreadClass myThreadClassObject  = new MyThreadClass(this);
         myThreadClassObject.Run();
      }
   }
   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }
      String myString;

      public void Run()
      {

         for (int i = 1; i <= 5; i++)
         {
            myString = "Step number " + i.ToString() + " executed";
            Thread.Sleep(400);
            // Execute the specified delegate on the thread that owns
            // 'myFormControl1' control's underlying window handle with
            // the specified list of arguments.
            myFormControl1.Invoke(myFormControl1.myDelegate,
                                   new Object[] {myString});
         }
      }
   }
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class MyFormControl
   Inherits Form

   Delegate Sub AddListItem(myString As String)
   Public myDelegate As AddListItem
   Private myButton As Button
   Private myThread As Thread
   Private myListBox As ListBox

   Public Sub New()
      myButton = New Button()
      myListBox = New ListBox()
      myButton.Location = New Point(72, 160)
      myButton.Size = New Size(152, 32)
      myButton.TabIndex = 1
      myButton.Text = "Add items in list box"
      AddHandler myButton.Click, AddressOf Button_Click
      myListBox.Location = New Point(48, 32)
      myListBox.Name = "myListBox"
      myListBox.Size = New Size(200, 95)
      myListBox.TabIndex = 2
      ClientSize = New Size(292, 273)
      Controls.AddRange(New Control() {myListBox, myButton})
      Text = " 'Control_Invoke' example "
      myDelegate = New AddListItem(AddressOf AddListItemMethod)
   End Sub

   Shared Sub Main()
      Dim myForm As New MyFormControl()
      myForm.ShowDialog()
   End Sub

   Public Sub AddListItemMethod(myString As String)
      myListBox.Items.Add(myString)
   End Sub

   Private Sub Button_Click(sender As Object, e As EventArgs)
      myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
      myThread.Start()
   End Sub

   Private Sub ThreadFunction()
      Dim myThreadClassObject As New MyThreadClass(Me)
      myThreadClassObject.Run()
   End Sub
End Class

Public Class MyThreadClass
   Private myFormControl1 As MyFormControl

   Public Sub New(myForm As MyFormControl)
      myFormControl1 = myForm
   End Sub
   Private myString As String

   Public Sub Run()

      Dim i As Integer
      For i = 1 To 5
         myString = "Step number " + i.ToString() + " executed"
         Thread.Sleep(400)
         ' Execute the specified delegate on the thread that owns
         ' 'myFormControl1' control's underlying window handle with
         ' the specified list of arguments.
         myFormControl1.Invoke(myFormControl1.myDelegate, New Object() {myString})
      Next i

   End Sub
End Class

설명

대리자는 C 또는 C++ 언어의 함수 포인터와 비슷합니다. 대리자는 대리자 개체 내의 메서드에 대한 참조를 캡슐화합니다. 그런 다음 참조된 메서드를 호출하는 코드에 대리자 개체를 전달할 수 있으며 컴파일 시 호출할 메서드를 알 수 없습니다. C 또는 C++의 함수 포인터와 달리 대리자는 개체 지향적이고 형식이 안전하며 더 안전합니다.

컨트롤의 핸들이 아직 없는 경우 이 메서드는 창 핸들이 있는 컨트롤 또는 폼을 발견할 때까지 컨트롤의 부모 체인을 검색합니다. 적절한 핸들을 찾을 수 없는 경우 이 메서드는 예외를 throw합니다. 호출 중에 발생하는 예외는 호출자에게 다시 전파됩니다.

참고

속성 외에도 InvokeRequired 스레드로부터 안전한 Invoke컨트롤에는 , , BeginInvokeEndInvokeCreateGraphics 및 컨트롤에 대한 핸들이 이미 만들어진 경우 네 가지 메서드가 있습니다. 백그라운드 스레드에서 컨트롤의 핸들을 만들기 전에 를 호출 CreateGraphics 하면 잘못된 스레드 간 호출이 발생할 수 있습니다. 다른 모든 메서드 호출의 경우 호출 메서드 중 하나를 사용하여 컨트롤의 스레드에 대한 호출을 마샬링해야 합니다.

대리자는 의 EventHandlerinstance 수 있습니다. 이 경우 보낸 사람 매개 변수에는 이 컨트롤이 포함되고 이벤트 매개 변수에는 가 포함EventArgs.Empty됩니다. 대리자는 의 instance MethodInvoker또는 void 매개 변수 목록을 사용하는 다른 대리자일 수도 있습니다. 또는 MethodInvoker 대리자를 EventHandler 호출하면 다른 유형의 대리자를 호출하는 것보다 빠릅니다.

참고

메시지를 처리해야 하는 스레드가 더 이상 활성 상태가 아니면 예외가 throw될 수 있습니다.

추가 정보

적용 대상

Invoke<T>(Func<T>)

컨트롤의 내부 창 핸들이 있는 스레드에서 지정된 대리자를 실행합니다.

public:
generic <typename T>
 T Invoke(Func<T> ^ method);
public T Invoke<T> (Func<T> method);
member this.Invoke : Func<'T> -> 'T
Public Function Invoke(Of T) (method As Func(Of T)) As T

형식 매개 변수

T

의 반환 형식입니다 method.

매개 변수

method
Func<T>

컨트롤의 스레드 컨텍스트에서 호출할 함수입니다.

반환

T

호출되는 함수의 반환 값입니다.

적용 대상