2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The essence of Invoke is just a method, and the method must be called through an object.
Generally speaking, Invoke can be used in only two situations:
Invoke of Control
Delegate Invoke
That is to say, Invoke is preceded by either a control or a delegate object.
Control's Invoke is generally used to solve the problem of cross-thread access. For example, if you want to operate a button, you have to use button.Invoke. If you want to operate a text label, you have to use label.Invoke. But everyone will find it very troublesome. If I want to operate both the button and the label, can I write them together? Of course you can.
We know that the main form is a Form, and Form naturally inherits Control, so Form also has an Invoke method, which can directly call Form.Invoke, which is the common this.Invoke. This is why there is a problem that there is nothing in front of Invoke. In fact, it is preceded by this, but it is omitted.
Example:
this.Invoke(new Action(() =>{ button1.Text = "关闭";}));
Delegate's Invoke actually calls the delegate method from the thread pool for execution. Invoke is a synchronous method and will block the UI thread that calls it.
- void PrintMessage(string message)
- {
- Console.WriteLine(message);
- }
- MyDelegate myDelegate = PrintMessage;
- myDelegate.Invoke("Hello, World!"); // 使用 Invoke 方法调用委托引用的方法`
We already know that C# has async/await, BackGroudWorker class and TPL (Task Parallel Library). Of course, C# also has some old patterns to support asynchronous programming.
- delegate long MyDel(int first, int second);
-
- class Program
- {
- static long Sum(int x, int y)
- {
- Console.WriteLine("------Inside Sum@{0}", DateTime.Now.ToString());
- Thread.Sleep(2000);
- return x + y;
- }
-
- static void Main(string[] args)
- {
- MyDel del = new MyDel(Sum);
-
- Console.WriteLine("Before BeginInvoke---@{0}", DateTime.Now.ToString());
- IAsyncResult iar = del.BeginInvoke(3, 5, null, null);
- Console.WriteLine("After BeginInvoke@{0}", DateTime.Now.ToString());
-
- Console.WriteLine("Doing stuff@{0}", DateTime.Now.ToString());
-
- long result = del.EndInvoke(iar);
- Console.WriteLine("End Invoke@{0}", DateTime.Now.ToString());
-
- Console.WriteLine("After EndInvoke: {0}", result);
-
- Console.ReadKey();
-
- }
- }
As shown in the above code, a delegate MyDel is defined, and the Sum method is passed to its object when it is called. Generally, when we call this delegate object, it will call the methods contained in its call list. Just like calling a method, this is done synchronously.
But if the delegate object has only one method (reference method) in the call list, it can execute this method asynchronously. BeginInvoke and EndInvoke are used to do this. We can use it in the following way:
* ① When we call the BeginInvoke method, it starts executing the referenced method on a separate thread and immediately returns to the original thread. The original thread can continue, and the referenced method will be executed in parallel in the desired thread.
* ②When the program wants to obtain the result of a completed asynchronous method, it can check the IsCompleted property of the IAsyncResult returned by BeginInvoke, or call the EndInvoke method of the delegate to wait for the delegate to complete execution.
The above usage process leads to three modes:
* ①Wait-until completion After the original thread initiates the asynchronous method and does some other processing, the original thread is interrupted and waits for the asynchronous method to complete before continuing.
* ② Polling: The original thread periodically checks whether the initiated thread is completed. If not, it can continue to do other things.
* ③ The callback original thread is executed all the time without waiting or checking whether the initiated thread is completed. After the reference in the initiated thread is issued, the initiated thread will call the callback method, and the callback method will process the result of the asynchronous method before calling EndInvoke.
C# thread delegate BeginInvoke and EndInvoke usage _C# tutorial _ script home