异步概述Async Overview

不久前,人们通过购买更新的电脑和服务器来加快应用的速度,但是这种趋势已经停止了。Not so long ago, apps got faster simply by buying a newer PC or server and then that trend stopped. 事实上,趋势逆转了。In fact, it reversed. 手机配备 1ghz 单核 ARM 芯片,而服务器工作负荷转向 VM。Mobile phones appeared with 1ghz single core ARM chips and server workloads transitioned to VMs. 用户仍青睐响应式 UI,企业所有者则希望拥有能随着其业务缩放的服务器。Users still want responsive UI and business owners want servers that scale with their business. 向手机和云的转变以及超过 30 亿使用 Internet 的人口引领着新软件模式的形成。The transition to mobile and cloud and an internet-connected population of >3B users has resulted in a new set of software patterns.

  • 有了高应用存储率,客户端应用有望保持始终开启,始终连接的状态,并且可持续响应用户交互(例如,触摸)!Client applications are expected to be always-on, always-connected and constantly responsive to user interaction (for example, touch) with high app store ratings!
  • 用户期望服务能够通过平稳地扩展和收缩来应对流量高峰。Services are expected to handle spikes in traffic by gracefully scaling up and down.

异步编程是一项关键技术,可以直接处理多个核心上的阻塞 I/O 和并发操作。Async programming is a key technique that makes it straightforward to handle blocking I/O and concurrent operations on multiple cores. 通过 C#、Visual Basic 和 F# 中易于使用的语言级异步编程模型,.NET 可为应用和服务提供使其变得可响应且富有弹性。.NET provides the capability for apps and services to be responsive and elastic with easy-to-use, language-level asynchronous programming models in C#, Visual Basic, and F#.

为什么要编写异步代码?Why Write Async Code?

新型应用广泛使用文件和网络 I/O。Modern apps make extensive use of file and networking I/O. 默认情况下 I/O API 一般会阻塞,导致糟糕的用户体验和硬件利用率,除非希望学习和使用富有挑战的模式。I/O APIs traditionally block by default, resulting in poor user experiences and hardware utilization unless you want to learn and use challenging patterns. 基于任务的异步 API 和语言级异步编程模型改变了这种模型,只需了解几个新概念就可默认进行异步执行。Task-based async APIs and the language-level asynchronous programming model invert this model, making async execution the default with few new concepts to learn.

异步代码具有以下特点:Async code has the following characteristics:

  • 等待 I/O 请求返回的同时,可通过生成处理更多请求的线程,处理更多的服务器请求。Handles more server requests by yielding threads to handle more requests while waiting for I/O requests to return.
  • 等待 I/O 请求的同时生成 UI 交互线程,并通过将长时间运行的工作转换到其他 CPU 核心,让 UI 的响应速度更快。Enables UIs to be more responsive by yielding threads to UI interaction while waiting for I/O requests and by transitioning long-running work to other CPU cores.
  • 许多较新的 .NET APIs 都是异步的。Many of the newer .NET APIs are asynchronous.
  • 在 .NET 中编写异步代码很简单!It's easy to write async code in .NET!

后续步骤What's next?

有关详细信息,请参阅异步深度剖析主题。For more information, see the Async in depth topic.

异步编程模式主题概述了 .NET 支持的三个异步编程模式:The Asynchronous Programming Patterns topic provides an overview of the three asynchronous programming patterns supported in .NET:

若要详细了解推荐的基于任务的编程模型,请参阅基于任务的异步编程主题。For more information about recommended task-based programming model, see the Task-based asynchronous programming topic.