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 版

第8章. 相互接続

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

非同期、パラレル、リアクティブ-それぞれに適材適所があるが、どの程度うまく連携しているのだろうか?

この章では、様々な相互運用のシナリオを見て、これらの異なるアプローチをどのように組み合わせればよいかを学ぶ。あるアプローチが別のアプローチと出会う境界線では、ほとんど摩擦がない。

8.1 "Completed "イベントを持つ "非同期 "メソッドの非同期ラッパー

問題

は古い非同期パターンで、次の名前のメソッドを使う。 OperationAsyncという名前のイベントを使う。 OperationCompleted.古い非同期パターンを使って演算を行い、その結果をawait

チップ

である。 OperationAsyncOperationCompletedパターンはイベントベース非同期パターン(EAP)と呼ばれている。これらを、Task-Task-based Asynchronous Pattern (TAP)に従ったメソッドにラップするのだ。

解決策

TaskCompletionSource<TResult> 型を使用することで、 、非同期操作のラッパーを作成することができる。TaskCompletionSource<TResult> 型はTask<TResult> を制御し、適切なタイミングでタスクを完了させることができる。

この例では、string をダウンロードするWebClient の拡張メソッドを定義している。WebClient 型はDownloadStringAsyncDownloadStringCompleted を定義している。これらを使って、DownloadStringTaskAsync メソッドを定義することができる:

public static Task<string> DownloadStringTaskAsync(this WebClient client,
    Uri address)
{
  var tcs = new TaskCompletionSource<string>();

  // The event handler will complete the task and unregister itself.
  DownloadStringCompletedEventHandler handler = null;
  handler = (_, e) =>
  {
    client.DownloadStringCompleted -= handler;
    if (e.Cancelled)
      tcs.TrySetCanceled();
    else if (e.Error != null)
      tcs.TrySetException(e.Error);
    else
      tcs.TrySetResult(e.Result);
  };

  // Register for the event and *then* start the operation.
  client.DownloadStringCompleted += handler;
  client.DownloadStringAsync(address);

  return tcs.Task;
}

ディスカッション

というのも、WebClient はすでにDownloadStringTaskAsync を定義しており、async より使いやすい ...

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