为什么要使用异步/等待过正常线程或任务? [英] Why Use Async/Await Over Normal Threading or Tasks?

查看:294
本文介绍了为什么要使用异步/等待过正常线程或任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读了很多关于异步和等待,起初我并没有得到它,因为我没有正确理解线程或任务。但是,掌握这两个后,我在想:为什么要使用异步/计谋如果你熟悉线程

I've been reading a lot about async and await, and at first I didn't get it because I didn't properly understand threading or tasks. But after getting to grips with both I wonder: why use async/await if you're comfortable with threads?

的异步/等待asynchronousy可以仅仅是为了节省时间编码和少麻烦做的是线程的信号,或的Thread.join()等,是什么呢?

The asynchronousy of async/await can be done with Thread signaling, or Thread.Join() etc. Is it merely for time saving coding and "less" hassle?

推荐答案

是的,这是使使用线程处理要容易得多一个语法糖,这也使得code更容易维护,因为线程管理是做通过运行时间。 等待立即释放该线程并允许那个线程或另一个拿起离开的地方,即使做了主线程。

Yes, it is a syntactic sugar that makes dealing with threads much easier, it also makes the code easier to maintain, because the thread management is done by run-time. await release the thread immediately and allows that thread or another one to pick up where it left off, even if done on the main thread.

像其他抽象的,如果你想完全控制在幕后的机制,那么你仍然可以自由使用线程的信号等来实现类似的逻辑。

Like other abstractions, if you want complete control over the mechanisms under the covers, then you are still free to implement similar logic using thread signaling, etc.

如果您有兴趣看到的异步/计谋产生的,那么你可以使用反射器或ILSpy反编译生成的code。

If you are interested in seeing what async/await produces then you can use Reflector or ILSpy to decompile the generated code.

阅读什么异步和放大器;等待产生?对什么C#5.0的描述正在代表你。

Read What does async & await generate? for a description of what C# 5.0 is doing on your behalf.

这篇关于为什么要使用异步/等待过正常线程或任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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