问题描述
这个网页,,在螺纹部分,说一个异步方法不会在其它线程中运行,如果我想使用其他的线程,我会用Task.Run。
于是我明白,异步和线程是两个diferents的东西,每一个选项是良好的一些情况。我想知道什么时候是最好使用异步时,最好使用线程。
感谢。
您使用线程,当你有恒定的工作要做。无论是直接OFR用定制编写池。 。即使如此,你可以把它隐藏自定义任务计划的背后(用他自己的线程池)
主题有一定的优势,当溜溜在低级别的线程参数Uneed公司控制 - 这是非常罕见的。比如设置优先级的东西琐事的东西,你还可以在异步做的(记得要重新设置),但有时你ened设立颇有些东西互操作。
不过,这些天线程是一个非常低的水平API - 因为任务都是围绕定制调度,你真的很少采用自定义任务计划程序(可能使用定制的线程内部低层次的API的线程池)外螺纹
主题也派上用场的时候哟uallocate长期的线程。长期不necessarly计算密集型。我这里有一个API运行在自定义线程24小时循环 - 我启动一个线程,调用成回调到我一个过程数据的方法。该方法返回无论是在问题/错误,或每天一次(要立即重新启动下一个实时数据块)。显然,忙碌不停,这就是一个很好的例子一个线程,而不是一个任务,因为任务的所有先进功能是没用的。
有关几乎一切否则这些天,我用任务/异步。
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.
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.
这篇关于异步线程VS,何时使用每个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!