In Silverlight, to marshall a call to the UI thread we are supposed to use this.Dispatcher.BeginInvoke((ThreadStart)delegate(){}).
My question is if multiple threads call a function which does this then do those calls get queued up and execute one after another? If yes, then can I safely assume that even though the function is getting called from multiple threads, the code inside the this.Dispatcher.BeginInvoke((ThreadStart)delegate(){} ); is thread safe ?
Code is thread-safe when it has no state, doesn’t modify state that can be shared across thread boundaries or modifies state in a controlled manner that all users of that state share in order to ensure thread safety (such as locks).
Therefore, there is no guarantee of thread safety from
Dispatcher.BeginInvoke. That said, it is guaranteed that the delegates will all execute on the same thread (the UI thread) so you can assume that the delegates will not run concurrently. This does not mean they are inherently thread-safe – that depends on what you do in those delegates – but if you don’t spin or interact with other threads from those delegates or methods called by those delegates, you can assume thread-safety.