ASync与线程? [英] ASync vs Threads?

查看:85
本文介绍了ASync与线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中观看了一组关于ASYNCHRONOUS编程的培训视频(在pluralsight.com上)。  听起来非常像THREADING。  有什么区别?  我应该何时使用其中一个(仅举几个例子)?



Don

解决方案

异步编程是一种在内部使用线程的高级构造。所以你将永远使用Threads;不同之处在于,如果您使用Async,则无需处理async为您处理的一些低级别详细信息。例如,当您编写UWP应用程序(或MVC中的异步控制器)时,
 并调用var x = await MyFunctionAsync(),基础结构知道如何创建线程(来自ThreadPool) ,启动MyFuncition,将控制权返回到继续运行的主
线程,然后在等待之后,当另一个线程结束时,继续执行。如果你是从Thread手动编写它,这种类型的来回线程切换需要大量的编码工作,但async
infrastucture以几乎透明的方式处理它。当然,如果你不需要任何这个并且你只想创建一个Thread并启动它,那么好的旧System.Threading.Thread会很好地完成它并且你不需要打扰Async。

I've watched a set of training videos about ASYNCHRONOUS programming in C# (on pluralsight.com).  It sounds an awful lot like THREADING.  What is the difference?  When should I use one vs the other (just a few examples)?


Don

解决方案

Asynchronous programming is a higher-level construct that uses Threads internally. So you will always be using Threads; the difference is that if you use Async you won't have to deal with some low level details that async takes care of for you. For instance, when you are writing a UWP application (or an asynchronous controller in MVC, for instance) and call var x = await MyFunctionAsync(), the infrastructure knows how to create a Thread (from the ThreadPool), launch MyFuncition, return control to the main thread that continues running, end then resume execution after await when the other thread has ended. This type of back-and-forth thread switching would require significant coding effort if you were to write it manually from a Thread, but the async infrastucture handles it in an almost transparent way. Of course, if you don't need any of this and you just want to create a Thread and launch it, the good old System.Threading.Thread will do it nicely and you don't need to bother about Async.


这篇关于ASync与线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆