In this article:
http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx
The author uses the following method to make thread-safe calls to a Windows For
This may be obvious to most, but you can take the accepted answer and add another method if you need to retrieve the value...
public static T SynchronizedFunc(this ISynchronizeInvoke sync, Func func)
{
if (!sync.InvokeRequired)
{
// Execute the function
return func();
}
// Marshal onto the context
return (T) sync.Invoke(func, new object[] { });
}
I used this recently to get handle of the form in a thread-safe way...
var handle = f.SynchronizedFunc(() => f.Handle);