WPF Threading Issues

Source: Internet
Author: User

An example of reading today,

1  Private voidAddMessage (stringformatString,2            params string[] Parameters)3         {4Dispatcher.begininvoke (NewAction (5() =WatchOutput.Items.Insert (6                   0,string. Format (formatString, Parameters )));7}
View Code

What is Dispatcher.begininvoke, and why do you do it? Why don't you just use

1 WatchOutput.Items.Insert (0string. Format (formatString, parameters));
View Code

So I did an experiment, when using the following statement, when you click on the Watch button, there is no problem, but when I modify the properties of the corresponding file, trigger the event, this time error.

not processed InvalidOperationException " because other threads have this object, the calling thread cannot access it. "

Surfing the internet, it turned out to be a threading problem, WPF applications typically have two threads, one for rendering (rendering UI), and one for managing the UI. The rendering thread is effectively hidden in the background, while the UI thread accepts input, handles events, draws the screen, and runs application code.

Then why does it have this problem? Click Watch, there is no problem, but when you modify the file properties and other operations, there will be problems. This is because some event handlers, when the event is triggered, when the code executes into the event handler, the thread that executes the code is often not the thread, and because of the protection of the UI control, the UI process creates the control, other threads are not able to access the UI line thread things.

As you can see below, the execution of an event is another thread.

1) When the watch! button is clicked, the Click event is triggered, and the thread at this time is the main threaded (UI thread)

2) A new worker thread is created when modifying file properties, triggering events such as created\changed.

Note: Not all event handlers will create new threads here, as can be seen from the example above.

So since only the main thread can modify the UI, how should other threads interact with the user? Other threads can request that the UI thread perform operations on his behalf, that is, by registering work items with dispatcher, dispatcher provides two ways to register work items: Invoke (synchronous) and BeginInvoke (asynchronous).

As seen from the above example, in order to add text to the interface, the event is registered with the Dispather (using the LAMDA expression).

In the system we use, Invoke is also used: when the next business is displayed. Create a new process through Parameterizedthreadstart in the main thread. To modify the content on the interface in this process, you can only invoke the Invoke method.

WPF Threading Issues

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.