非同期オプション
非同期コールバック メソッドでタイマーを使用して、たとえば 5 秒間バイトが受信されない場合に操作を完了することができます。バイトを受信するたびにタイマーをリセットします。BeginRead の前に開始します。
同期オプション
または、基になるソケットの ReceiveTimeout プロパティを使用して、読み取りが完了するまでの最大待機時間を設定できます。より大きなバッファを使用して、タイムアウトを 5 秒などに設定できます。
プロパティが同期読み取りにのみ適用されるMSDN ドキュメントから。別のスレッドで同期読み取りを実行できます。
アップデート
これは、同様の問題からまとめられた、テストされていない大まかなコードです。おそらくそのままでは実行されません(またはバグがありません)が、アイデアが得られるはずです。
private EventWaitHandle asyncWait = new ManualResetEvent(false);
private Timer abortTimer = null;
private bool success = false;
public void ReadFromTwitter()
{
abortTimer = new Timer(AbortTwitter, null, 50000, System.Threading.Timeout.Infinite);
asyncWait.Reset();
input.BeginRead(buffer, 0, buffer.Length, InputReadComplete, null);
asyncWait.WaitOne();
}
void AbortTwitter(object state)
{
success = false; // Redundant but explicit for clarity
asyncWait.Set();
}
void InputReadComplete()
{
// Disable the timer:
abortTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
success = true;
asyncWait.Set();
}