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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With