Invoke and begininvoke

Source: Internet
Author: User

The delegate is used in the use of invoke or begininvoke without exception.

I. Why?ControlClass providesInvokeAndBegininvokeMechanism?

The main reason for this problem is DOTNET.ProgramAs we all know, I have recorded my logs again here, so that I can remind myself later.

1,WindowsProgram message mechanism

The Windows GUI program is based on the message mechanism, and a main thread maintains a message pump. This message pump keeps Windows programs running.

A Windows program has a message queue. All messages in the form are the primary sources of messages in this queue. Here, the while loop uses the getmessage () method. This is a blocking method, that is, the method will be blocked when the queue is empty, so that the while loop stops motion, this avoids a program from exhausting the CPU for no reason, making it difficult for other programs to respond. Of course, you can use another method in some programs that require maximum CPU movement. For example, in some 3D games or strategy games, peekmessage () is generally used, it will not be blocked by windows, so as to ensure the smoothness and High Frame Rate of the entire game.

The main thread maintains the entire form and the child controls above. When it gets a message, it will call the dispatchmessage method to dispatch the message, which will cause the call to the Window Process on the form. In the window process, the form data is updated by the programmer.CodeAnd other code.

2,DOTNETMessage Loop

Public static void main (string [] ARGs)

{

Form F = new form ();

Application. Run (f );

}

The DOTNET form program encapsulates the preceding while loop, which is started through the application. Run method.

3, Out-of-thread operationsGuiControl Problems

If you operate controls on Windows forms from another thread, it will compete with the main thread, causing unpredictable results and even deadlocks. Therefore, Windows GUI programming has a rule that allows you to operate control data only by creating the control thread. Otherwise, unexpected results may occur.

Therefore, in DOTNET, to solve these problems easily, the control class implements the isynchronizeinvoke interface and provides the invoke and begininvoke methods to allow other threads to update GUI controls.

Public interface isynchronizeinvoke

{

[Hostprotection (securityaction. linkdemand, synchronization = true, externalthreading = true)]

Iasyncresult begininvoke (delegate method, object [] ARGs );

Object endinvoke (iasyncresult result );

Object invoke (delegate method, object [] ARGs );

Bool invokerequired {Get ;}

}

}

If you operate a Windows form control outside the thread, you need to use the invoke or begininvoke method and use a delegate to mail the call to the thread to which the control belongs for execution.

Ii. Message Mechanism---Communication between threads and processes

1,WindowMessage sending

Windows message mechanism is one of the threads or inter-process communication mechanisms on Windows. Windows message value is actually a defined data structure. The most important thing is the message type, which is an integer and then the message parameter. Message parameters can represent many things.

Windows provides APIs for sending messages to a thread message queue. Therefore, a thread can send messages to the Message Queue of another thread to tell the other thread what to do, thus completing inter-thread communication. Some APIs require a window handle to send messages. This function can send messages to the main thread message queue of the specified window, while some can directly use the thread handle, send messages to the thread message queue.

Sendmessage is a Windows API used to send a message to a window message queue. This method is a blocking method, that is, the operating system will ensure that the message is indeed sent to the destination message queue, and the function will return after the message is processed. The caller will be temporarily blocked before return.

Postmessage is also an API function used to send messages to the window message queue, but this method is not blocking. That is, it will return immediately, regardless of whether the message is actually sent to the destination, that is, the caller will not be blocked.

The invoke or begininvoke method requires a delegate object as the parameter. The delegate is similar to the callback function address. Therefore, the caller can use these two methods to block the function address to the interface thread. If these methods contain code for changing the control status, the interface thread is used to avoid competition conditions and unexpected problems. If other threads directly operate on the control to which the interface thread belongs, competition conditions will be generated, resulting in unpredictable results.

Using invoke to complete the sending of a delegate method is similar to using the sendmessage method to send messages to the interface thread. It is a synchronous method. That is to say, the invoke method will not return until the invoke method is executed, and the caller thread will be blocked.

Use the begininvoke method to mail a delegate method, similar to using postmessage for communication, which is an Asynchronous Method. That is, the method will be returned immediately after the method is sent. It will not wait until the execution of the delegate method ends, and the caller thread will not be blocked. However, the caller can also use the endinvoke method or other similar waithandle mechanism to wait for the completion of asynchronous operations.

In terms of internal implementation, both invoke and begininvoke use the postmessage method to avoid problems caused by sendmessage. The synchronous blocking of the invoke method relies on the waithandle mechanism.

3Usage issues

If your background thread does not need to wait after updating the status of a UI control, but needs to continue processing, you should use begininvoke for asynchronous processing.

If your background thread needs to operate the UI control and wait until the operation is completed to continue, you should use invoke. Otherwise, if the background thread and the main cross-section thread share certain State data, if they do not call the data synchronously but continue to execute the data, the execution sequence may be faulty, although no deadlock occurs, unexpected display results or data processing errors may occur.

We can see that isynchronizeinvoke has an attribute called invokerequired. This attribute is used to determine whether invoke or begininvoke is required for an object to access the UI control during programming. If you do not need it, you can directly update it. This attribute returns false when the caller object and UI object belong to the same thread. In the code analysis, we can see that the implementation of the control class for this attribute is to determine whether the caller and the control belong to the same thread.

III,Delegate. begininvoke

Asynchronous calling of synchronous methods through a delegate is also one of the asynchronous calling mechanisms provided by. net. However, the delegate. begininvoke method extracts a thread from the threadpool to execute this method to obtain the asynchronous execution effect. That is to say, if multiple asynchronous delegates are submitted in this way, the order of these calls cannot be guaranteed. In addition, because the threads in the thread pool are used to complete tasks, frequent use may affect the system performance.

Delegate. begininvoke also refers to sending a delegate method to other threads, and thus executing a method through asynchronous mechanism. The caller thread can continue its work after the mail is completed. However, the final execution thread sent by this method is a thread selected by the Runtime library from the threadpool.

A misunderstanding needs to be corrected here, that is, the asynchronous call of the control class begininvoke does not open up a new thread to complete the delegate task, but rather enables the thread of the interface control to complete the delegate task. It seems that asynchronous operations are not necessarily accurate to open up new threads.

Finally, we can see that this is to judge whether the Windows form thread and the current caller thread are the same. If it is the same, there is no need to mail it out. Visit this GUI control directly. Otherwise, you don't need to say so directly. You need to use invoke or begininvoke as the media.

V. Use of C # multi-thread invoke Method

In multi-threaded programming, we often need to update the interface display in the working thread. In multithreading, it is wrong to call interface controls directly, invoke and begininvoke occur to solve this problem, so that you can safely update the interface display in multiple threads.

The correct method is to encapsulate the code that involves updating the interface in the work thread as a method.InvokeOrBegininvokeThe difference between the two is that one causes the worker thread to wait, while the other does not.

The so-called "one-side response operation, one-side node addition" can always be relative, so that the burden on the UI thread is not too great, because correct interface updates must always be done through the UI thread, what we need to do is to wrap most of the operations in the work thread, and put the pure interface updates into the UI thread for the purpose of reducing the burden on the UI thread.

Http://www.cnblogs.com/tigeryl/archive/2010/12/13/1905066.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.