I want to have a function to something similar:
public static V callAsyncAndWait<V>(Func<V> func)
{
ThreadPool.QueueUserWorkItem(obj =>
{
V v = func.Invoke();
});
return v;
}
Obviously this code doesn’t compile. What I want is to run the Func in another thread and return the result. How can I do that?
I recommend you to use the new .NET 4.0
Taskclass instead. Here is a tutorial on how to return a result from the execution ofTask: http://msdn.microsoft.com/en-us/library/dd537613.aspxPractically you have a very convenient property called
Result, which, upon invocation of the getter, will block until the result is available.