Neil Barnwell
Neil Barnwell

Reputation: 42165

.NET SynchronizationContext - Which thread does it Send/Post to?

I'm planning on using the SynchronizationContext class to perform some cross-thread marshalling of UI updates. The idea is to avoid having to have a reference to the main form (i.e. the one in Application.Run(form)) just so I can say mainForm.BeginInvoke();

However, one thing that isn't clear from the documentation, is that when you call SynchronizationContext.Post(), which thread it marshalls the call to. Is it always the main application thread, or the thread that first initialised a SynchronizationContext object, or what?

Upvotes: 2

Views: 1789

Answers (2)

MaLio
MaLio

Reputation: 2540

Depends on the type of SynchronizationContext. when you extend the class you can implement the method invokation on any thread you like (more or less). The windows forms one will mashal back to the main ui thread. The wpf will use the dispatcher thread and so on.

Upvotes: 2

Neil Barnwell
Neil Barnwell

Reputation: 42165

Ahh, CodeProject has something useful:

http://www.codeproject.com/KB/threads/SynchronizationContext.aspx

I'll take a look at that.

UPDATE: Turns out that the main thread is given a SynchronizationContext object when the first form is opened on it. It is this sync context's thread that the send and post calls are marshalled to.

Upvotes: 0

Related Questions