Invoke C# Là Gì

  -  

Some information relates to prerelease product that may be substantially modified before it’s released. thanglon77.com makes no warranties, express or implied, with respect to the information provided here.

Bạn đang xem: Invoke c# là gì


In this article

Overloads

Invoke(Action)

Executes the specified delegate on the thread that owns the control"s underlying window handle.

Invoke(Delegate)

Executes the specified delegate on the thread that owns the control"s underlying window handle.

Invoke(Delegate, Object<>)

Executes the specified delegate, on the thread that owns the control"s underlying window handle, with the specified danh mục of arguments.

Invoke(Func)

Executes the specified delegate on the thread that owns the control"s underlying window handle.

Xem thêm: Ăn Gì Để Dễ Tiêu Hóa - Rối Loạn Tiêu Hóa Nên Ăn Gì


public: void Invoke(kích hoạt ^ method);public void Invoke (Action method);thành viên this.Invoke : kích hoạt -> unitPublic Sub Invoke (method As Action)Parameters

public: System::Object ^ Invoke(Delegate ^ method);public object Invoke (Delegate method);member this.Invoke : Delegate -> objPublic Function Invoke (method As Delegate) As ObjectParameters
Returns Object

The return value from the delegate being invoked, or null if the delegate has no return value.

Examples

The following code example shows controls that contain a delegate. The delegate encapsulates a method that adds items to lớn the danh mục box, & this method is executed on the thread that owns the underlying handle of the form. When the user clicks on the button, Invoke runs the delegate.

/*The following example demonstrates the 'Invoke(Delegate*)' method of 'Control class.A 'ListBox' & a 'Button' control are added lớn a size, containing a delegatewhich encapsulates a method that adds items to lớn the listbox. This function is executedon the thread that owns the underlying handle of the form. When user clicks on buttonthe above sầu delegate is executed using 'Invoke' method.*/#using #using #using using namespace System;using namespace System::Drawing;using namespace System::Windows::Forms;using namespace System::Threading;public ref class MyFormControl: public Formpublic: delegate void AddListItem(); AddListItem^ myDelegate;private: Button^ myButton; Thread^ myThread; ListBox^ myListBox;public: MyFormControl(); void AddListItemMethod() String^ myItem; for ( int i = 1; i 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' và a 'Button' control are added to a form,// containing a delegate which encapsulates a method that adds items khổng lồ the listbox.public ref class MyThreadClassprivate: MyFormControl^ myFormControl1;public: MyThreadClass( MyFormControl^ myForm ) myFormControl1 = myForm; void Run() // Exexinh đẹp 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 các mục box"; myButton->Cliông xã += gcnew EventHandler( this, &MyFormControl::Button_Cliông chồng ); 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^ 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' & a 'Button' control are added lớn a size, containing a delegatewhich encapsulates a method that adds items to the listbox. This function is executedon the thread that owns the underlying handle of the form. When user clicks on buttonthe above sầu 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 danh mục box"; myButton.Cliông xã += 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' The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.' A 'ListBox' & a 'Button' control are added lớn a size, containing a delegate' which encapsulates a method that adds items khổng lồ the listbox. This function is executed' on the thread that owns the underlying handle of the size. When user clicks on button' the above sầu delegate is executed using 'Invoke' method.Imports System.DrawingImports System.Windows.FormsImports System.ThreadingPublic 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 menu box" AddHandler myButton.Clichồng, 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() Dyên myForm As New MyFormControl() myForm.ShowDialog() End Sub Public Sub AddListItemMethod() Dlặng myItem As String Dyên 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 SubEnd Class' The following code assumes a 'ListBox' and a 'Button' control are added lớn a khung, ' containing a delegate which encapsulates a method that adds items lớn the listbox.Public Class MyThreadClass Private myFormControl1 As MyFormControl Public Sub New(myForm As MyFormControl) myFormControl1 = myForm End Sub Public Sub Run() ' Exexinh tươi the specified delegate on the thread that owns ' 'myFormControl1' control's underlying window handle. myFormControl1.Invoke(myFormControl1.myDelegate) End SubEnd Class

Remarks

Delegates are similar lớn function pointers in C or C++ languages. Delegates encapsulate a reference lớn a method inside a delegate object. The delegate object can then be passed khổng lồ code that calls the referenced method, và the method khổng lồ be invoked can be unknown at compile time. Unlượt thích function pointers in C or C++, delegates are object-oriented, type-safe, & more secure.

Xem thêm: Last Year Là Thì Gì - Cấu Trúc Và Cách Dùng Các Thì Tiếng Anh

The Invoke method searches up the control"s parent chain until it finds a control or khung that has a window handle if the current control"s underlying window handle does not exist yet. If no appropriate handle can be found, the Invoke method will throw an exception. Exceptions that are raised during the Gọi will be propagated bachồng khổng lồ the caller.