When you use WPF development, you often encounter threads that you create that need to update the interface UI content, resulting in cross-threading issues.
Exception content:
Exception type: System.InvalidOperationException
Exception Description:
Unhandled exception of type "System.InvalidOperationException" occurs in WindowsBase.dll
Additional information: The calling thread cannot access this object because another thread owns the object.
One of the easiest ways to solve this problem in WPF is to use dispatcher.
1, the most convenient use of dispatcher
This. Dispatcher.invoke (New action (() = {//do something//update UI Action})); Thread.Sleep (100);
2. Using the dispatcher of the control itself "in WPF, the control finally inherits from the Dispatcherobject"
if (!this.pb_test. Dispatcher.checkaccess ()) {//Update UI interface This.pb_test. Dispatcher.invoke (Dispatcherpriority.normal, new updateprogressbardelegate (int progress) = {This.pb_test. Value = Progress;}), i); Thread.Sleep (100);}
3, with 2, using the dispatcher of the current form
if (! Dispatcher.checkaccess ()) {Dispatcher.invoke (dispatcherpriority.normal,new updateprogressbardelegate (int Progress) = {This.pb_test. Value = Progress;}), i); Thread.Sleep (100);}
Project Escrow Address: https://wpfthread.codeplex.com/
WPF thread--dispatcher