Usually we invoke on UI thread that way :
myControl.Invoke((MethodInvoker)delegate() { Foo(); });
Are they any way to do it without any control instance ? I mean something like this :
System.Windows.Forms.Thread.Invoke(...);
Solution
Solution given by dcastro, using WindowsFormsSynchronizationContext
. Here is a really simple example in a form :
public partial class FrmFoo : Form
{
SynchronizationContext uiThread;
public void FrmFoo()
{
// Needs to assign it from somewhere in the UI thread (in constructor for example)
uiThread = WindowsFormsSynchronizationContext.Current;
}
void AsyncBar()
{
uiThread.Send(delegate(object state)
{
// UI Cross-Thread dangerous manips allowed here
}, null);
}
}