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版)
互操作
93
你可能会问,为什么推荐模式总是在最后传递
null
.NET 4.0
引入了
FromAsync
Task
类型,此时尚不存在
async
。那时,
state
对象通常用于异步回调,而且
Task
类型通过其
AsyncState
成员支持这种功能。但是,对新的
async
形式而言,因为
state
对象已然不再
是必需的,所以始终向
state
参数传递
null
是合理的。如今只有在优化内存使用时,才会
使用
state
来避免闭包实例。
参阅
8.3
节探讨为任意类型的通知编写
TAP
包装器。
8.3
 异步包装任意对象
问题
假设有不寻常或非标准的异步操作或事件,现在需要用
await
来消耗它。
解决方案
TaskCompletionSource<T>
类型可用于在任意场景中构造
Task<T>
对象。使用
TaskCompletion-
Source<T>
可以通过
3
种方式完成任务:得到成功、错误或已取消的结果。
async
尚未出现之前,微软推荐过其他两种异步模式:
APM
(参见
8.2
节)和
EAP
(参
8.1
节)。然而,这两种模式相当复杂,而且在某些棘手的情况下难以使用。因此,一种
非官方的常规做法应运而生,其中使用了回调及如下代码中的方法:
public interface
IMyAsyncHttpService
{
void
DownloadString(Uri address, Action<
string
, Exception> callback);
}
像这样的方法遵循了
DownloadString
会启动(异步)下载的惯例,当它完成时 ...
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