Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.Factory.StartNew() vs. TaskEx.Run()

Task.Factory.StartNew() basically receives an Action and returns a Task. In The Async CTP we have TaskEx.Run() which also receives an Action and returns a Task. They seem to do that same thing. Why TaskEx.Run() was introduced ?

like image 758
Yaron Levi Avatar asked May 25 '11 11:05

Yaron Levi


People also ask

What does task factory StartNew do?

StartNew(Action<Object>, Object, CancellationToken, TaskCreationOptions, TaskScheduler) Creates and starts a task for the specified action delegate, state, cancellation token, creation options and task scheduler.

Does Task run create a new thread?

NET code does not mean there are separate new threads involved. Generally when using Task. Run() or similar constructs, a task runs on a separate thread (mostly a managed thread-pool one), managed by the . NET CLR.

What is the use of task factory in C#?

The TaskFactory class allows you to do the following: Create a task and start it immediately by calling the StartNew method. Starting with . NET Framework 4.5, the Task.

Does Task run Use thread pool?

Inside DoComplexCalculusAsync(), Task. Run uses another new thread from thread pool to do the heavy calculations in the background. Thus the thread pool has to deal with unexpectedly loosing one thread from the pool.


2 Answers

Anders Hejlsberg talked about that briefly in an interview on Channel9. Apparently, Task.Run is just a shorthand for Task.Factory.StartNew. Its still early CTP days so we're unsure that Task.Run will make it int. I personally hope it won't because it's kind of redundant. :)

like image 99
aL3891 Avatar answered Oct 06 '22 20:10

aL3891


Stephen Toub covered it in his article. They are the same, one being shorthand for the other.

like image 31
Bruno Brant Avatar answered Oct 06 '22 20:10

Bruno Brant