INVOKE C# LÀ GÌ

  -  

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

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


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 các mục of arguments.

Invoke(Func)

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

Xem thêm: Cfi Là Gì - Trong Xuất Nhập Khẩu


public: void Invoke(Action ^ method);public void Invoke (Action method);member this.Invoke : action -> 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 list 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 to lớn a form, 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 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' & a 'Button' control are added to a form,// containing a delegate which encapsulates a method that adds items lớn the listbox.public ref class MyThreadClassprivate: 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 các mục 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^ 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 lớn a form, containing a delegatewhich encapsulates a method that adds items lớn the listbox. This function is executedon the thread that owns the underlying handle of the form. When user clicks on buttonthe above delegate is executed using 'Invoke' method.*/using System;using System.Drawing;using System.Windows.Forms;using System.Threading; public class MyFormControl : khung { 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' The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.' A 'ListBox' & a 'Button' control are added lớn a form, containing a delegate' which encapsulates a method that adds items lớn 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.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 các mục 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() kết thúc 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() over Sub Private Sub ThreadFunction() Dim myThreadClassObject As New MyThreadClass(Me) myThreadClassObject.Run() end SubEnd Class' The following code assumes a 'ListBox' và a 'Button' control are added to lớn a form, ' containing a delegate which encapsulates a method that adds items to lớn the listbox.Public Class MyThreadClass Private myFormControl1 As MyFormControl Public Sub New(myForm As MyFormControl) myFormControl1 = myForm over Sub Public Sub Run() ' Execute 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 to lớn function pointers in C or C++ languages. Delegates encapsulate a reference to lớn a method inside a delegate object. The delegate object can then be passed khổng lồ code that calls the referenced method, & the method lớn be invoked can be unknown at compile time. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, & more secure.

Xem thêm: Tìm Hiểu Về Convolutional Neural Networks Là Gì ? Convolutional Neural Network Là Gì

The Invoke method searches up the control"s parent chain until it finds a control or form 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 back to lớn the caller.