user642770
user642770

Reputation: 415

Dispatcher.BeginInvoke c#, silverlight

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 ?

Upvotes: 1

Views: 2913

Answers (4)

Jeff Yates
Jeff Yates

Reputation: 62407

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.

Upvotes: 3

justin.m.chase
justin.m.chase

Reputation: 13685

Your code snippets are a bit misleading because of the use of the (ThreadStart) delegate type. The important thing to know is that in any STA Threaded environment there is exactly one thread blessed to be the "UI" thread. All UI objects need to be created and interacted with on that thread.

So the Dispatcher is used for several things the most easy to understand usage is to use it from a background thread to put what you're calling back onto the UI thread. So what you put in there isn't exactly "thread safe" but it is guaranteed to be called on the UI thread. If you put everything you do onto the UI thread it will be called, one after another, not at the same time.

BeginInvoke is an asynchronous call to put a delegate on the UI thread queue while Invoke is a syncrhonous call. What's really mind boggling is that you can actually call Invoke from the UI thread which will block, put your delegate onto the queue and yield to the next items in the queue, eventually calling what it just Invoke'd.

Another important thing to keep in mind is that the Dispatcher is actually a Prioritized queue. So it's not a pure queue in the sense of first-in-first-out because where you are inserted into the queue is based on your priority and it's entirely possible for enough things to be jammed into the queue that your invoked method never gets executed.

Upvotes: 1

Stuart
Stuart

Reputation: 66882

In general, yes, you are correct that each delegate gets added to the queue to await processing by the Dispatcher thread.

I'm not sure if there is any current or future guarantee on ordering on the invocation of the delegate from the Dispatcher.BeginInvoke() calls - but I do know that currently the ordering seems to be preserved.

Regardless of this though - yes, it is safe to assume that there will be only Dispatcher (UI) thread - so multiple delegates won't be invoked at the same time.

Upvotes: 0

user1228
user1228

Reputation:

Yes, these calls get queued up. Calls to BeginInvoke are thread-safe (you can call this method from any thread at any time), and all calls marshaled to the UI thread all run single threaded. Because there is only one UI thread. Because.

Upvotes: 0

Related Questions