I'd like to have some work done in a background thread as simple as creating a Future var for it and then asking later for the calculated value.
In pseudo-C#-code:
AsyncFuture<int> asyncFuture = new AsyncFuture<int>(FuncToCalculateValue);
//do some other work, or draw UI
if(asyncFuture.NoErrorsHappened){
int realResult = asyncResult.Value;
}
I can implement such type by my own, but my question is: isn't that some kind of a known pattern? Is there maybe a name for it, or maybe even a framework implementation? Probably in .NET 4.0?
And if it is a pattern, what are the pitfalls associated with it?
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
Yes, Futures are part of the Task Parallel Library which will be in .NET 4.0.
In .NET 4.0 Beta 1, it looks like this exists as a Task<TResult>
.
Yacoder, I really like Ayende's two Future implementations. There is some good discussion from Jon Skeet in the comments on the cons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With