Skip to Content
C#并发编程经典实例(第2版)
book

C#并发编程经典实例(第2版)

by Stephen Cleary
November 2020
Intermediate to advanced
226 pages
5h 34m
Chinese
Posts & Telecom Press
Content preview from C#并发编程经典实例(第2版)
异步编程基础
29
async
Task ResumeWithoutContextAsync()
{
await
Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(
false
);
// 该方法在恢复时丢弃其上下文
}
讨论
UI
线程中运行过多的延续任务,可能引发性能问题。此类性能问题难以诊断,这是因为
致使系统变慢的并非单个方法。因为应用程序变得更加复杂,所以
UI
性能“遍体鳞伤”。
真正的问题在于,在
UI
线程中有多少延续任务才算“过多”?这个问题并没有固定的答
案,但微软公司的
Lucian Wischik
公布过
Universal Windows
团队采用的指导方针:每秒上
百个尚可,每秒上千个则过多。
最好从一开始就避免这个问题,针对任意无须在原始上下文中恢复的
async
方法,使用
ConfigureAwait
方案,这样做不会产生任何弊端。
在编写
async
代码时,了解上下文也是一个好主意。通常,
async
方法要么需要上下文
(处理
UI
元素或
ASP.NET
请求和响应),要么不受上下文影响(执行后台操作)。如果有
一个
async
方法,其中部分需要上下文,部分不需要上下文,那么可以考虑将其拆分为两
个或多个
async
方法,这样在组织代码时,层次会更为明晰。
参阅
1
章介绍了异步编程的基础知识。
2.8
 
async Task
方法的异常处理
问题
异常处理对所有设计来说都是至关重要的一环。针对成功情况的设计实现起来简单,但只
有把失败情况也处理得当,才算设计正确。幸运的是, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre

Publisher Resources

ISBN: 9787115550606