Skip to Content
Concurrency in C# Cookbook、第 2 版
book

Concurrency in C# Cookbook、第 2 版

by Stephen Cleary
May 2025
Intermediate to advanced
254 pages
3h 28m
Japanese
O'Reilly Media, Inc.
Content preview from Concurrency in C# Cookbook、第 2 版

第6章. System.Reactiveの基本

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

LINQは、開発者がシーケンスをクエリできるようにする言語機能のセットである。最も一般的なLINQプロバイダーは、組み込みのLINQ to Objects(IEnumerable<T> に基づく)とLINQ to Entities(IQueryable<T> に基づく)の2つである。他にも多くのプロバイダーがあり、ほとんどのプロバイダーは同じ一般化構造を持っている。クエリは遅延評価され、シーケンスは必要に応じて値を生成する。概念的には、これはプルモデルであり、評価中にクエリから値アイテムが1つずつ引き出される。

System.Reactive(Rx)、イベントは時間と共に到着するデータのシーケンスとして扱われる。そのため、RxをLINQ to Events(IObservable<T> )と考えることができる。可観測性と他のLINQプロバイダーの主な違いは、Rxが "プッシュ "モデルであることである。RxはLINQの上に構築され、拡張メソッドとしていくつかの強力な演算子を追加している。

この章()では、より一般的なRx演算子について説明する。LINQ演算子もすべて利用できるので、フィルタリング(Where)やプロジェクション(Select)のような単純な操作は、他のLINQプロバイダと概念的には同じように動作する。ここでは、これらの一般的なLINQ演算子は取り上げない。RxがLINQの上に構築する新しい機能、特に時間を扱う機能に焦点を当てる。

System.Reactiveを使うには、NuGetパッケージの System.Reactiveをアプリケーションにインストールする。

6.1 .NETイベントを変換する

問題

イベントをSystem.Reactiveの入力ストリームとして扱い、イベントが発生するたびにOnNext 、何らかのデータを生成する必要がある。

解決策

Observable クラスは、いくつかのイベント・コンバーターを定義している。ほとんどの.NETフレームワークのイベントはFromEventPattern と互換性があるが、共通パターンに従わないイベントがある場合は、代わりにFromEvent を使うことができる。

FromEventPattern は、イベント委譲の型が である場合に最もうまく機能する。多くの新しいフレームワーク・タイプは、このイベント委譲タイプを使用する。例えば、 型は、 イベントを定義している。これは、 型なので、 で簡単にラップできる:EventHandler<T> Progress<T> ProgressChanged EventHandler<T> FromEventPattern

var progress = new Progress<int>();
IObservable<EventPattern<int>> progressReports =
    Observable.FromEventPattern<int>(
        handler => progress.ProgressChanged += handler,
        handler => progress.ProgressChanged -= handler);
progressReports.Subscribe(data => Trace.WriteLine("OnNext: ...
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

Async in C# 5.0

Async in C# 5.0

Alex Davies

Publisher Resources

ISBN: 9798341650060