To use the Task
constructor that accepts an object state
argument you must have a function that accepts an object
argument too. Generally this is not convenient. The reason that this constructor exists is for avoiding the allocation of an object (a closure) in hot paths. For normal usage the overhead of closures is negligible, and avoiding them will complicate your code for no reason. So this is the constructor you should use instead:
public Task (Func<TResult> function);
...with this lambda as argument:
() => GetIntAsync("3")
There is one peculiarity in your case though: the lambda you pass to the constructor returns a Task<int>
. This means that the generic type TResult
is resolved to Task<int>
, and so you end up with a nested task:
var t = new Task<Task<int>>(() => GetIntAsync("3"));
Starting the outer task will result to the creation of the inner task. To get the final result you'll have to use the await
operator twice, one for the completion of the outer task, and one for the completion of the inner task:
static async Task Main(string[] args)
{
var outerTask = new Task<Task<int>>(() => GetIntAsync("3"));
//...
outerTask.Start(TaskScheduler.Default); // Run on the ThreadPool
//outerTask.RunSynchronously(TaskScheduler.Default) // Run on the current thread
//...
Task<int> innerTask = await outerTask; // At this point the inner task has been created
int result = await innerTask; // At this point the inner task has been completed
}