views:

267

answers:

1

The idomatic way to start a new side-effect-only task (that is: a task that returns no result) using the TPL in .NET 4.0 is using the following API:

Task Task.Factory.StartNew(Action<object>, object)

But why doesn't the signature of this API look like this

Task Task.Factory.StartNew<T>(Action<T>, T)

or like this

Task Task.Factory.StartNew<T>(T, Action<T>)

Technical reasons or some other reason?

+4  A: 

Okay, now that I understand the question properly :)

I believe it's because this is meant to be a direct replacement for ThreadPool.QueueUserWorkItem. I do agree that it seems somewhat odd... but if you're using lambda expressions anyway, it's probably easier to use the version that does take a state parameter (i.e. Action instead of Action<object>) and just capture the value you're interested in beforehand. It doesn't help if you're specifying the value and the function separately :(

Jon Skeet
Yes, but Task<T> is generic in the result type (TResult) of a task, but not generic in the type of the "initial state" (that is: the input for a task).
Novox
So... when I call Task.Factory.StartNew does it grab a thread from the threadpool automagically?
Padu Merloti
@Padu: Yes, I believe so - although you can have your own task factory which uses a different set of threads, I believe.
Jon Skeet