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.BeginInvokemethod will run its callback on the Dispatcher thread (typcially the UI thread, unless you have multiple Dispatchers)Therefore, if you only use the
countervariable on the UI thread, you will have no threading issues.