C# - When to use threads instead of tasks

I have not yet found a situation where I needed a thread instead of a Task. Threads are a lower level concept when compared to tasks. They are built into your operating system and the thread class is a way to manage these threads. Tasks are a higher level concept and a more convenient way to asynchronously execute a function.

Of course tasks use threads behind the scenes using the Thread pool, but in this post I am comparing using the Thread or Task classes directly.

There are several reasons to use tasks instead of threads, this is taken from my previous post on tasks VS threads:

  • Leveraging the thread pool: tasks use the thread pool, which is a "pool" of threads that can be used and reused. Creating threads can be expensive, which is why we have the thread pool.
  • Threads do not naturally return anything: Tasks are able to return an object when they are completed. Which makes them great for executing a method and returning the result asynchronously.
  • Cancellation tokens: Tasks can use cancellation tokens so that they can be requested to be canceled. This token can be passed along to other tasks which will be canceled as well.E
  • Tasks support async/await: async/await is a simple way to wait for asynchronous method to finish without blocking the thread.

If you know a good reason to use threads instead of tasks, please let me know in the comments down below! There may be situations I have not encountered yet.

I hope you enjoyed this post!