スレッドを処理しているスレッドが処理エンジンに受信したメッセージを転送しています。何らかの不都合が生じた場合(ソケットが予期せず閉じられた場合など)、そのスレッドは終了する「親」であることをどのように通知する必要がありますか?スレッドが親に予期せず終了したことを通知する方法
UPDATE:たとえば、ここでの問題の簡単なillistrationです:この例では
class Program
{
private static BlockingCollection<string> queue = new BlockingCollection<string>();
static void Main(string[] args)
{
Thread readingThread = new Thread(new ThreadStart(ReadingProcess));
readingThread.Start();
for (string input = queue.Take(); input != "end"; input = queue.Take())
Console.WriteLine(input);
Console.WriteLine("Stopped Listening to the queue");
}
static void ReadingProcess()
{
string capture;
while ((capture = Console.ReadLine()) != "quit")
queue.Add(capture);
// Stop the processing because the reader has stopped.
}
}
は、どちらかそれは「終わり」を見ているか、読み取り処理があるため終了すると、主なforループの外に終了"終了する"と見る。どちらのスレッドもブロックされています(ReadLineに1つ、Takeに1つ)
Martinのアドバイスに従うと、ReadingProcessはBlockingQueueと "end"に追加できます。この段階では、キューをすぐに停止したいと考えています。