Hi,
"THREAD_UI" = thread under which the UI controls were created.
"THREAD_XX" = another thread.
"FUNC_UI" = function that needs to run on THREAD_UI.
From THREAD_XX I need to start the execution of FUNC_UI, that needs to
run on THREAD_UI. Inside FUNC_UI, I will modify many controls present
on the form. Since I don't want to use Invoke() or BeginInvoke() for
each of those calls, this is why I thought it would be more efficient
if FUNC_UI run on THREAD_UI.
How, from THREAD_XX, can I invoke FUNC_UI to run on THREAD_UI ?
FUNC_UI is not tied to any single control. Invoke() and BeginInvoke()
are member methods of one control. Is there an elegant way to invoke
FUNC_UI without having to refer to a specific control?
Thank you very much,
Jon
Marc Gravell - 18 Mar 2008 21:42 GMT
One option is to pass an ISynchronizeInvoke into your code; any handy
Control will satisfy this, and it abstracts the way of calling Invoke
etc.
The other approach is to use the ambient sync-context; the running
form will set itself up as the sync-context:
SynchronizationContext ctx =
SynchronizationContext.Current;
if (ctx == null)
{ // no sync-context: execute directly
Foo();
}
else
{ // ask the sync-context to execute it for us
ctx.Send(delegate { Foo(); }, null);
}
Marc