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版)
74
6
运算符,还能轻松地进行过滤。不妨把
Throttle
Sample
看作与
Where
类似的运算符,
只不过这两者根据时间窗口而不是事件数据执行过滤。这
3
种运算符都能够以各自的方式
“驯化”高速的输入流。
参阅
6.1
节探讨从事件中创建序列。
6.2
节探讨向上下文发送通知。
6.5
 超时
问题
假设你希望事件能在一定的时间内到达,并需要确保即便事件未能到达,程序也会及时响
应。在大多数情况下,这类事件是单个异步操作,比如从
Web
服务请求获取响应。
解决方案
Timeout
运算符会在其输入流上创建滑动的超时窗口。每当有新事件到来时,该超时窗口就会
重置。如果超时过期,却未在窗口中发现事件,
Timeout
运算符就会以包含
TimeoutException
OnError
通知终止该流。
下面的示例发起了针对样例域名的
Web
请求,并采用了
1
秒的超时窗口。为了启动
Web
请求,代码使用了
ToObservable
Task<T>
转换为
IObservable<T>
(参见
8.6
):
void
GetWithTimeout(HttpClient client)
{
client.GetStringAsync(
"
http://exampleurl").ToObservable()
.Timeout(TimeSpan.FromSeconds(1))
.Subscribe(
x => Trace.WriteLine($"{DateTime.Now.Second}: Saw {x.Length}"),
ex => ...
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