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 版

第2章. 非同期の基本

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

このの章では、asyncawait を使った非同期操作の基本を紹介する。ここでは、HTTPリクエスト、データベースコマンド、Webサービス呼び出しなどの演算子である、当然の非同期操作のみを扱う。

CPU負荷の高い演算子を非同期のように扱いたい場合(UIスレッドをブロックしないようにするなど)は、第4章と レシピ8.4を参照のこと。イベントのストリームを処理する必要がある場合は、第3章と第6章を参照のこと。

2.1 一時停止する

問題

(非同期で)一定時間待つ必要がある。これは単体テストや再試行遅延を実装するときによくあるシナリオだ。また、単純なタイムアウトをコーディングするときにも出てくる。

解決策

Task 型には、指定された時間後に完了するタスクを返す静的メソッドDelay がある。

以下のコード例では、非同期に完了するタスクを定義している。非同期演算子を偽装する場合、同期的成功と非同期的成功、そして非同期的失敗をテストすることが重要である。以下の例では、非同期成功のケースで使われるタスクを返している:

async Task<T> DelayResult<T>(T result, TimeSpan delay)
{
  await Task.Delay(delay);
  return result;
}

Exponentialbackoffは、再試行間の遅延を増やす戦略である。再試行でサーバが溢れないようにするために、ウェブサービスを扱うときに使用する。次の例は、指数バックオフの簡単な実装である:

async Task<string> DownloadStringWithRetries(HttpClient client, string uri)
{
  // Retry after 1 second, then after 2 seconds, then 4.
  TimeSpan nextDelay = TimeSpan.FromSeconds(1);
  for (int i = 0; i != 3; ++i)
  {
    try
    {
      return await client.GetStringAsync(uri);
    }
    catch
    {
    }

    await Task.Delay(nextDelay);
    nextDelay = nextDelay + nextDelay;
  }

  // Try one last time, allowing the error to propagate.
  return await client.GetStringAsync(uri);
}
チップ

、プロダクション・コードには、NuGetライブラリーのような、より徹底したソリューションをお勧めする。 Pollyこのコードは、Task.Delay の簡単な使用例に過ぎない。

Task.Delay を単純なタイムアウトとして使うこともできる。CancellationTokenSource はタイムアウトの実装に使われる通常の型である(レシピ10.3)。キャンセル・トークンを無限Task.Delay 、指定した時間後にキャンセルするタスクを提供する。最後に、そのタイマー・タスクをTask.WhenAny (レシピ2.5)と一緒に使って、"ソフト・タイムアウト "を実装する。以下のコード例は、サービスが3秒以内に応答しない場合、 ...

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