views:

64

answers:

1

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 ++;
});
+4  A: 

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.

SLaks