I have a WPF app that makes use of some multi threading. I am curious to know if calling into the UI 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.