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版)
94
8
else
tcs.TrySetResult(result);
});
return
tcs.Task;
}
讨论
可以通过
TaskCompletionSource<T>
模式包装任意异步方法,即使这种方法是非官方的也可以。
首先创建
TaskCompletionSource<T>
实例,然后安排回调函数,这样
TaskCompletionSource<T>
能恰当地完成任务。接着,启动实际的异步操作。最后,返回附属于
TaskCompletionSource<T>
Task<T>
必须确保
TaskCompletionSource<T>
总能完成,这一点非常关键。尤其是要想清楚如何处理
错误,确保
TaskCompletionSource<T>
能妥善地完成。在上例中,因为异常显式传递至回调
函数,所以不需要
catch
块,但某些非官方模式可能需要在回调函数中捕获异常,并把它
们放到
TaskCompletionSource<T>
中。
参阅
8.1
节探讨用于
EAP
Operation
Async
Operation
Completed
)的
TAP
包装器。
8.2
节探讨用于
APM
Begin
Operation
End
Operation
)的
TAP
包装器。
8.4
 异步包装并行代码
问题
假设想用
await
消耗
CPU
密集型并行处理。这种做法通常是可取的,这样
UI
线程就不会
阻塞,等待并行处理完成。
解决方案
Parallel
类型和
PLINQ
使用线程池执行并行处理。它们也会包含调用线程,将其作为并
行处理线程之一。因此,如果从 ...
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