WPF + XMPP-based im program development log 2 WPF thread model

Source: Internet
Author: User

In the beginning of the text, I will talk nonsense. As the topic of this blog-development log, this series of blogs is some logs of this IM, or another saying: notes. Not some system articles, such as "XX learning tutorial ". The content in these blogs mainly records the problems I encountered and my understanding of the problems, so as not to go to Google again when I encounter these problems in the future, rather than some tutorials on "How to Write im programs.

 

I. WPF thread model.

Unless you are familiar with the WPF architecture and multi-threaded development, we often encounter this exception when dealing with WPF:

Because other threads own this object, the calling thread cannot access it. (The Calling thread cannot access this object because a different thread owns it)

In WPF, two threads are inherent. One thread is used to render the UI, And the other thread is used to manage the UI (this is called the UI thread ). It is said that the animation effect of Android is not as good as that of iPhone, because the rendering thread of iPhone has a very high priority, as long as there are animation operations, such as sliding a menu, this animation is scheduled to run at the highest priority to ensure smooth animation. I have not studied this in depth, so the above description may not be very correct technically, but it can be understood in this way. This is also the idea in WPF. The UI thread creates controls defined in XAML or in C # And owns them. Other threads cannot access things in the UI thread out of UI protection, if we create a new thread and modify a button defined in XAML or in the main thread. content.

During the IM development process, when the agsxmpp library is used, agsxmpp has many events, such as xmppclientconnection. onstatechanged events, onerror events, and so on, we will use many event processing functions. Here we must note that when these events are triggered and the code is executed in the event processing function, the thread that executes the code is often not the main thread (here I often don't know if it is used correctly, but what I encounter is not executed in the main thread). That is to say, if such code is written in the event processing function: button1.content = "something", the calling thread cannot access the object because other threads own the object. When debugging the code in Visual Studio, we can see whether the current code is executed by the main thread or other threads: If the thread column does not specify the main thread ", the current thread for code execution is not the main thread.

 

In this case, what should we do if we have to access the control in other threads? This requires dispatcher. Most of the controls in WPF inherit from dispatcherobject, and they also have the dispatcher attribute. I will not write this dispatatcher because I don't know it either, but in a nutshell, it is such an object for Job Scheduling on the thread to which it belongs, or a manager or intermediary of the thread. To access the control of a thread from an external (or other threads) with a control, you can only use the dispatcher of the control. The dispatcher has two methods: invoke and begininvoke are used to open the opportunity to access the control of the thread to which the dispatcher belongs. For example, if I want to access the button of the main thread in another thread:

 

 

Private void oneventfired (Object sender, mousebuttoneventargs E)

{
BTN. Dispatcher. Invoke (new action (delegate {button1.content = "some text ";}));
}

Assume that oneventfired is being executed by other threads. In the code, an (anonymous) function is executed through the invoke method of BTN. Dispatcher. This function sets buttton1.content. Invoke and begininvoke are instant and asynchronous calls.

 

 Ii. thread. apartmentstate

Sometimes we encounter this exception: the call thread must be Stas, because many UI components are required. (The Calling thread must be STA, because component UI components require this)

This is because in WPF, not all threads can create controls, or in WPF, not all controls can be created in non-stathread. The full name of this sta is single thread apartment, and the other corresponds to MTA (multi thread apartment ). The STA thread is a concept in COM (for details, Google), because the underlying level of WPF should be associated with COM. When a new control is created, some places are hooked up with COM, therefore, to create a new control for a thread, this thread usually needs to be set to sta (thread. apartmentstate attribute ). Why can a new control be created in the main thread? Because the main thread is a stathread.

 

 

The above mainly describes some concepts and technical points of WPF, but since I have not studied it very deeply, many of the above content may be wrong, even if I understand it this way. On the other hand, I personally feel that I want to solve technical problems, but I still want to go to some foreign websites, such as stackoverflow.com, to find some information. That's why I wrote the exception in English, it is easy to search in Google in enlish. Search for Chinese documents on Google and you will find that eight of the 10 articles are reposted in the ninth article, but the first article is just saying it, not saying why. When searching for some English websites, we can often see that the reply from foreigners is not just "mark", but there are also many reasons to go deep into it, or to explain in the detailed principles, so that we can better understand a certain technical point.

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.