In WPF is the UI dispatcher.begininvoke method thread safe?
I have a WPF app that makes use of some multi threading. I am curious to know if calling into the U开发者_如何学JAVAI thread by using the Dispatcher.BeginInvoke() method considered thread-safe? Normally i would use the lock statement to make sure only one thread can access a variable. Would the following be thread-safe in a WPF app?
this.Dispatcher.BeginInvoke(() =>
{
_counter ++;
});
The Dispatcher.BeginInvoke
method will run its callback on the Dispatcher thread (typcially the UI thread, unless you have multiple Dispatchers)
Therefore, if you only use the counter
variable on the UI thread, you will have no threading issues.
精彩评论