WPF dispatcherpriority Analysis

Source: Internet
Author: User

For a long time, dispatcherpriority has a mysterious color for me, because I don't even know how to "really" Use it.

 

I. Execution Principle

 

When using dispatcher. begininvoke, one of the parameters is to specify the appropriate dispatcherpriority. Previously, when setting this parameter, I often tried it by feeling or effort-a method that failed and then continued to try again. The purpose of the begininvoke function is to make the execution of the specified delegate in the parameter slower (not immediately executed at the time ), alternatively, you can avoid errors caused by the current thread's control operation during multi-threaded operations.

Of course, it is also possible to execute it right away. It is nothing more than specifying dispatcherpriority as send.

In fact, apart from sending, all other messages are sent in the form of message queue-use postmessage to send a custom message, and then judge and process the message when the next message queue reads the message.

What are the advantages of this method?

This can be asynchronous. Sometimes asynchronous operations are often associated with simultaneous operations. Here, the delegate operations to be executed in Dispatcher. begininvoke are stored somewhere, and the operations following dispatcher. begininvoke are executed first. The subsequent operation is executed, and then a picture will make the user feel that it is not too "card ".An important factor for the user's perception card is that the screen is not displayed timely.. After rendering is executed, remember to take out the delegate in Dispatcher. begininvoke for execution. What is the best method? Some may say that a timer is used to check whether it is after dispatcher. begininvoke.CodeFinished. But how do you know that the code is complete after dispatcher. begininvoke?

Who triggered the code before dispatcher. begininvoke?

When we put this code in the Click Event of a button, have you ever thought about the source of the click event? Yes, it is a message queue. Our getmessage, that is, when the code after dispatcher. begininvoke ends, it will return to the while loop of getmessage. What do you create a thread without a message loop and the button created in it does not respond? So I want a weak question. Are you sure you want to use the WPF framework? When you use another thread to create the UI, you must enable the message loop because it is the basis of windows.

 Now that we want to return to getmessage, we need to record the message to the Message below getmessage, so we use postmessage to send the message.

 

Ii. Execution Method

 

In addition to two invalid dispatcherpriority (inactive, invalid) and instant send, other types can be divided into two types: front-end and backend.

What is the front-end?

The frontend sends a postmessage custom message immediately.

What is the background?

In the background, if the currently executed system message is an input message, the system will not immediately send the postmessage message, but execute a timer. In fact, timer is also a message, but it is faster, when a message is no longer input, the postmessage is used to send the message like the foreground thread. Why do I have to wait until the input message ends? This is for more accurate input, such as Ctrl + C shortcut keys and continuous mouse clicks, so as not to cause bugs.

What are the foreground threads?

Normal, databind, render, and load are executed in the fastest to slowest order.

Why is the split size?

I just mentioned dispatcher. begininvoke stores the delegate in one place, which is certainly a place where the set can be recorded, because dispatcher is in a piece of code. begininvoke can be executed multiple times, and then the system receives a message to retrieve a data set. Which one should it take? It is obviously inappropriate to simply use a queue or stack. With dispatcherpriority, users can have more choices and opportunities for matching.

In order to arrange the order of retrieving from the set, why not use enumeration instead of simple numbers and get a name like render?

This is also the biggest problem I have ever been confused about. I wonder if the WPF framework has used some numerical permissions in advance and has done specific things in these numbers, so it is easier to name the specific things by name.

 

Iii. execution time

 

Normal:This is the most common and the highest limit. Invoke actually uses this.

Databind:Currently unknown, it seems that the name is related to data binding.

Render:Use postrender to send image data to the backend micore module (wpfgfx_v0300.dll 3.5.

Loaded:It mainly sends load, unload, events, and layout events. In fact, load and unload are also layout events. For example, a load event is triggered after the control is added to a visualtree, and unload is triggered after the control is removed from the visualtree. For example, the content block in the middle of tabcontrol is continuously switched.In the layout, we are still concerned about one thing, that is, when to set the template? In addition to manually calling applytemplate, how does the system call applytemplate? For a simple next breakpoint of this function, the answer is measurecore. Then how is measurecore triggered? The answer is that when a visualtree is added, the invalidatemeasure of the control is triggered. invalidatemeasure is asynchronous and does not affect the layout immediately, simply look at the source code with a tool and you will know that the function is used to put the control in a queue, but the control is not the current control, it is the root element found along the visual tree. In this way, the number of measure operations is reduced, because measure affects both parent and child controls. The transfer from the root element actually saves money. Therefore, it is reasonable to apply for a template in measurecore, because measure thinks it is necessary to know what the control application data looks like after applying for a space for layout.

Input:Input event

Background:In fact, after invalidatemeasure adds the root element to the set, it still needs not to be executed for the time being. Therefore, the asynchronous structure already has begininvoke. Naturally, the original structure is used, and the background is used. The reason is understandable. Generally, the input will cause layout changes. It is best to apply for a successful event in the template here or later.

Contextidle:It can detect garbage collection to avoid Memory leakage. For examples, see the last penultimate section of Self-pushing in my previous BLOG--WPF binding implementation.

Applicationidle:Invalidatemeasure

Systemidle:After the preceding steps are completed

 

Of course, these may be incorrect and will be supplemented later.

Related Article

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.