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版)
互操作
95
讨论
这是一个简单的方法,但经常被忽视。通过使用
Task.Run
,可以把所有的并行处理推进线
程池中。
Task.Run
返回的
Task
代表了并行工作,而
UI
线程可以异步等待其完成。
该方法只适用于
UI
代码。以服务器端为例,比如
ASP.NET
,因为服务器已经有并行机制
了,所以它很少执行并行处理。因此,服务器端的代码既不应执行并行处理,也不应将工
作推进线程池中。
参阅
2
章探讨异步编程基础。
4
章探讨并行编程基础。
8.5
 异步包装
System.Reactive
可观察对象
问题
假设有可观察流,需要用
await
来消耗。
解决方案
在详细研究解决方案之前,需要决定重点关注事件流中的哪些可观察事件,通常分为下面
3
种情况。
重点关注流结束前的最后一个事件。
重点关注下一个事件。
重点关注所有事件。
若要捕获流中的最后一个事件,可以对
LastAsync
的结果执行
await
或者直接用
await
待可观察对象:
IObservable<
int
> observable = ...;
int
lastElement =
await
observable.LastAsync();
// 或者用int lastElement = await observable;
当针对可观察对象或
LastAsync
执行
await
时,代码异步地等待,直到流完成,然后返回
最后的元素。其实在这个过程中,
await
也会订阅流。
若要捕获流中的下一个事件,可以使用
FirstAsync
。在下面的代码中,
await
订阅了流,
然后在第一个事件到达时,便立即完成(并取消订阅) ...
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