Simple Future<T> adaptor

In .NET 4.0 the Task class already performs asynchronous execution. Here’s a simple wrapper that matches the signature of Lazy<T>. A Future says executes this in the background and I’ll ask for the result later on.

public class Future<T> {
    Task<T> task ;

    public bool IsValueCreated {
      get { return task.IsCompleted ; } 
    }

    public T Value {
      get { return task.Result ; }
    }

    public override string ToString () {
      return Value.ToString() ;
    }

    public Future () : this (() => new T()) {}

    public Future (Func<T> constructor) {
      task = Task<T>.Factory.StartNew(constructor) ;
    }
}
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s