Reputation: 6699
I am reading about Tasks been preferred way of doing async programming with 4.0. I am just wondering if there are any use cases where use of Tasks should not be preferred over normal c# threads?
Upvotes: 9
Views: 309
Reputation: 613
This is gone into detail over here: Should I notice a difference in using Task vs Threads in .Net 4.0?
This biggest difference is that the TaskFactory uses thread pooling, so if you have a lot of tasks they may not start immediately. They have to wait for a free thread to run. In most cases this is acceptable..
Threads will run instantly as soon as .Start() is called, hardware permitting.
Assuming Thread pooling is okay, Tasks offer many benefits including cancellation, ContinueWith, OnSuccess, OnError, Exception aggregation, and WaitAll to name a few off the top of my head.
Upvotes: 6
Reputation: 62469
Since Task
s use the underlying ThreadPool
(unless marked as long running), it's a bad idea to use them whenever using a ThreadPool
is not advised e.g.
Upvotes: 10