Solve the problem through "LAN chat"

Source: Internet
Author: User

When talking about asynchronous calls, I believe many people are familiar with it. In "LAN chat", we usually divide this operation into many steps to make some method calls that spend a lot of time without affecting the UI response, and then use begininvoke to call each step, in this way, the UI response will not be blocked. The essence of begininvoke is to send a message to the Message Queue instead of calling it directly. At the same time, the UI behavior (mousemove) causes the system to update the UI in the message queue by using the postmessage. However, it takes a short time for each other to update the UI, the interface will not be blocked if two messages are processed at the same time. WPF also faces such a problem. How does it solve it? Here window 1 # plays a vital role. The following figure shows what window 1 is doing?

WPF also solves this problem through "LAN chat", while WPF's begininvoke is exposed on the dispatcher because the entire message system is being coordinated by dispatcher. The figure above shows the process that dispatcher experienced after calling begininvoke, and when Foo () was actually executed.

The first step is to package the called "LAN chat" and priority into a dispatcheroperation and put it into the priority queue maintained by the dispatcher. This queue is sorted by dispatcherpriority, dispatcheroperation is always processed first. For more information about priority, see msdn's explanation of the WPF thread model.

Step 2: post a message named msgprocessqueue to the Message Queue of the current thread. This message is defined by WPF itself. For details, see before the message in the static constructor of dispatcher is post to the message queue, set MSG. Handle, which is the handle of window 1. Handle is used to specify the window wndproc (Window Process) in which to process the message when the message is cyclically dispatch. All the messages caused by begininvoke are processed by the window1 # window process.

Step 3: read messages cyclically. Step 4: The system finds the same handle as window1 # based on the "LAN chat" for obtaining the message, so the message is distributed to the window in window1 # for processing. Step 5: Take a dispatcheroperation in the priority queue during the window process. Step 6: Execute the dispatcheroperation. Invoke method. The core of the invoke method is the delegate passed in when the dispatcheroperation construction is called, that is, the delegate passed in by dispatcher. begininvoke. Finally, the Foo () method is executed.

Through the above six steps, a "LAN chat" is processed. This process requires the continuous flow of messages, so you must join the message queue and finally handle specific window processes. The core thing is this hidden window1 #, in WPF, he is only responsible for processing asynchronous calls. He does not care about other messages, and the remaining four windows are being processed. This window1 # contains a shell in WPF. If you are interested, you can view the messageonlyhwndwrapper type.

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.