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 版

付録B. 非同期パターンの認識とインタプリタ

非同期コードの利点は、.NETが発明される何十年も前からよく理解されていた。.NETの黎明期には、いくつかの異なるスタイルの非同期コードが開発され、あちこちで使われたが、最終的には破棄された。これらの多くは、現代のasync/await 。しかし、世の中には古い非同期パターンを使っているレガシーコードがたくさんある。この付録では、より一般的なパターンについて説明し、それらがどのように機能し、どのように最新のコードと統合するかを説明する。

、同じ型が何年もかけて更新され、複数の非同期パターンをサポートするようになり、より多くのメンバーを獲得することもある。その最たる例がSocket 。以下は、Send のコアとなるSocket クラスのメンバの一部である:

class Socket
{
  // Synchronous
  public int Send(byte[] buffer, int offset, int size, SocketFlags flags);

  // APM
  public IAsyncResult BeginSend(byte[] buffer, int offset, int size,
      SocketFlags flags, AsyncCallback callback, object state);
  public int EndSend(IAsyncResult result);

  // Custom, very close to APM
  public IAsyncResult BeginSend(byte[] buffer, int offset, int size,
      SocketFlags flags, out SocketError error,
      AsyncCallback callback, object state);
  public int EndSend(IAsyncResult result, out SocketError error);

  // Custom
  public bool SendAsync(SocketAsyncEventArgs e);

  // TAP (as an extension method)
  public Task<int> SendAsync(ArraySegment<byte> buffer,
      SocketFlags socketFlags);

  // TAP (as an extension method) using more efficient types
  public ValueTask<int> SendAsync(ReadOnlyMemory<byte> buffer,
      SocketFlags socketFlags, CancellationToken cancellationToken = default);
}

悲しいことに、ほとんどのドキュメントはアルファベット順であり、使い方を単純化するために大量のオーバーロードがあるため、Socket のような型は理解しにくくなっている。このセクションのガイドラインが助けになることを願っている。

タスクベース非同期パターン(TAP)

Task-Based Asynchronous Pattern (TAP)は、await ですぐに使える最新の非同期APIパターンである。各非同期演算子は、awaitableを返す単一のメソッドで表される。awaitable」とは、await によって消費され得る任意の型のことである。これは通常、 ...

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