3

This page, http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx, in the thread section, says that a async method does not run in other thread, that if I want to use other thread, I would use Task.Run.

So I understand that async and threading are two diferents things, and each option is good for some situations. I would like to know when is better to use async and when is better to use threading.

Thanks.

1 Answer 1

3

You use threads when you have constant work to do. Either directly ofr with a custom written pool. And even then you may hide it behind a custom Task Scheduler (using his own thread pool).

Threads have SOME Advantages when yo uneed control over the low Level thread Parameters - which is VERY rare. Something trivia like Setting priority is something you also can do in async (remember to set back), but sometimes you ened to set up quite some things for interop.

Still, These days threads are a very low Level API - since Tasks are around with custom schedulers, you really have VERY Little uses for threads outside a custom Task Scheduler (which may use a thread pool of custom made threads internally as low Level API).

Threads also come in Handy when yo uallocate a thread for LONG TERM. Long term is not necessarly "computational intensive". I have an API here that runs in 24 hour Loops on a custom thread - I start a thread, call into a "process data" method which calls back to me. The method Returns either on issue / error, or once per day (to be immediately restarted for the next real time data block). Obviously, being busy non stop, this is a good case for a thread, not a Task as ALL the advanced Features of a Task would be useless.

For pretty much everything else These days I use a Tasks / async.

Sign up to request clarification or add additional context in comments.

4 Comments

and with async/awiat, I use all the CPUs or if I use all them I need many threads?
I think the question wasn't really about Task vs. Thread, but more about async vs. Task.Run().
well, when I say Task.Run is because the task create a new thread, and async not, if I am not wrong.
No, Actually async IS A TASK. It schedules a Task on the Default Scheduler - totally identical. Read up on async and what it means and what yo uget back when you call an async method. Compiler sugar around a Task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.