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 版

第10章 キャンセル キャンセル

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

.NET 4.0フレームワークでは、網羅的でよく設計されたキャンセル・サポートが導入された。このサポートは協調的であり、キャンセルを要求することはできるが、コードに強制することはできない。キャンセルは協調的であるため、キャンセルをサポートするようにコードを記述しない限り、コードをキャンセルすることはできない。このため、できるだけ多くのコードでキャンセルをサポートすることをお勧めする。

キャンセルは一種の信号であり、キャンセルのトリガーとなるソースと、キャンセ ルに応答するレシーバーという2つの異なる側面を持つ。.NETでは、ソースはCancellationTokenSource であり、レシーバーはCancellationToken である。この章のレシピは、通常の使用におけるキャンセルのソースとレシーバーの両方をカバーし、非標準のキャンセル形式と相互運用するためにキャンセルサポートを使用する方法について説明する。

キャンセル は特殊なエラーとして扱われる。キャンセルされたコードは、OperationCanceledException (またはTaskCanceledException のような派生型)の例外を投げるという慣例がある。こうすることで、呼び出し元のコードはキャンセルが観測されたことを知ることができる。

呼び出しコードにメソッドがキャンセルをサポートしていることを示すには、CancellationToken をパラメータとして取る必要がある。このパラメータは、メソッドが進行状況も報告する場合を除き、通常は最後のパラメータとなる(レシピ2.3)。キャンセルを必要としないコンシューマには、オーバーロードやデフォルト値のパラメータを提供することも考えられる:

public void CancelableMethodWithOverload(CancellationToken cancellationToken)
{
  // Code goes here.
}

public void CancelableMethodWithOverload()
{
  CancelableMethodWithOverload(CancellationToken.None);
}

public void CancelableMethodWithDefault(
    CancellationToken cancellationToken = default)
{
  // Code goes here.
}

CancellationToken.None は、決してキャンセルされないキャンセルトークンを表し、 と等価性のある特殊な値である。コンシューマは、演算子がキャンセ ルされることを望まない場合に、この値を渡す。default(CancellationToken)

非同期ストリームのキャンセルの処理方法は似ているが、より複雑である。非同期ストリームのキャンセルについては、レシピ3.4()で詳しく説明する。

10.1 キャンセル要請の発行

問題

あなたのコードはキャンセル可能なコード(それはCancellationToken を取る)を呼び出し、あなたはそれをキャンセルする必要がある。

解決策

CancellationTokenSource タイプは、CancellationToken のソースである。 ...

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