views:

456

answers:

2

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?

A: 

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.

Neil Barnwell
+2  A: 

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.

MaLio