views:

151

answers:

3

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?

+1  A: 

also in Java the structure of the code using Future is very similar.

You may be move the "do some other work or draw UI" as a parametric code? This can be viewed as a Template Method.

dfa
+3  A: 

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>.

Jeff Moser
+1  A: 

Yacoder, I really like Ayende's two Future implementations. There is some good discussion from Jon Skeet in the comments on the cons.

Anthony Mastrean