There are two classes available in .NET: Task and Thread.
- What is the difference between those classes?
- When is it better to use
ThreadoverTask(and vice-versa)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Threadis a lower-level concept: if you’re directly starting a thread, you know it will be a separate thread, rather than executing on the thread pool etc.Taskis more than just an abstraction of “where to run some code” though – it’s really just “the promise of a result in the future”. So as some different examples:Task.Delaydoesn’t need any actual CPU time; it’s just like setting a timer to go off in the futureWebClient.DownloadStringTaskAsyncwon’t take much CPU time locally; it’s representing a result which is likely to spend most of its time in network latency or remote work (at the web server)Task.Run()really is saying “I want you to execute this code separately”; the exact thread on which that code executes depends on a number of factors.Note that the
Task<T>abstraction is pivotal to the async support in C# 5.In general, I’d recommend that you use the higher level abstraction wherever you can: in modern C# code you should rarely need to explicitly start your own thread.