10.
例: バイト列読み込み
• 同期
static byte[] ReadBytes(Stream s, int n)
{
var buffer = new byte[n];
s.Read(buffer, 0, n);
return buffer;
}
ここでフリーズ
の可能性あり
11.
例: バイト列読み込み
• begin/end、コールバック式
static void ReadBytes(Stream s, int n, Action<byte[]> callback)
{
var buffer = new byte[n];
s.BeginRead(buffer, 0, n, r =>
{
var result = s.EndRead(r);
callback(buffer);
}, null);
}
2個のメソッドをペアで
呼ぶ必要あり
後ろにさらに処理がつづいたり、
分岐・ループさせるとかなり面倒
12.
例: バイト列読み込み
• ContinueWith※、コールバック式
static Task<byte[]> ReadBytes(Stream s, int n)
{
var buffer = new byte[n];
return s.ReadAsync(buffer, 0, n).ContinueWith(t => buffer);
}
※ 他のプログラミング言語だと Then という名前が多い
いわゆる継続処理(continuation)
後ろにさらに処理がつづいたり、
分岐・ループさせるとかなり面倒
Be the first to comment