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 版

第5章 データフローの基本 データフローの基本

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

TPLDataflowは、メッシュやパイプラインを作成し、そこに(非同期に)データを送ることができる強力なライブラリである。通常、まずメッシュを完全に定義してからデータ処理を始める。メッシュは結局、データが流れる構造になっている。このため、アプリケーションについて少し違った考え方をする必要があるが、一度その飛躍をすれば、データフローは多くのシナリオに自然に適合するようになる。

各メッシュは、互いにリンクされた様々なブロックで構成されている。個々のブロックはシンプルで、データ処理の単一ステップを担当する。あるブロックがデータ処理を終えると、その結果をリンクしているブロックに渡す。

TPL Dataflowを使用するには、NuGetパッケージをアプリケーションにインストールする。 System.Threading.Tasks.Dataflowをアプリケーションにインストールする。

5.1 ブロックのリンク

問題

メッシュを作成するには、データフロー・ブロックを互いにリンクさせる必要がある。

解決策

TPL Dataflowライブラリが提供するブロックは、最も基本的なメンバーのみを定義している。便利なTPL Dataflowメソッドの多くは、実際には拡張メソッドである。LinkTo 拡張メソッドは、データフローブロックを一緒にリンクする簡単な方法を提供する:

var multiplyBlock = new TransformBlock<int, int>(item => item * 2);
var subtractBlock = new TransformBlock<int, int>(item => item - 2);

// After linking, values that exit multiplyBlock will enter subtractBlock.
multiplyBlock.LinkTo(subtractBlock);

デフォルトでは、リンクされたデータフロー・ブロックはデータのみを伝搬し、完了(またはエラー)は伝搬しない。データフローが(パイプラインのように)線形である場合、おそらく完了を伝播したいだろう。完了(とエラー)を伝播するには、リンクにPropagateCompletion オプションを設定する:

var multiplyBlock = new TransformBlock<int, int>(item => item * 2);
var subtractBlock = new TransformBlock<int, int>(item => item - 2);

var options = new DataflowLinkOptions { PropagateCompletion = true };
multiplyBlock.LinkTo(subtractBlock, options);

...

// The first block's completion is automatically propagated to the second block.
multiplyBlock.Complete();
await subtractBlock.Completion;

ディスカッション

がリンクされると、ソースブロックからターゲットブロックへ自動的にデータが流れる。 ...

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