I have a thread that is listening on a socket and forwarding on the messages it receives to processing engine. If something untoward happen (such as the socket is unexpectedly closed) how should that thread notify it's "parent" that it's about to end?
UPDATE: For example, here's a simple illistration of the issue:
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.
}
}
In this example, either the Main finishes out of the for loop when it sees "end" or the reading process finishes because it sees "quit". Both threads are blocked (one on a ReadLine, and the other on a Take.
Following Martin's advice the ReadingProcess could add to the BlockingQueue and "end" --- however there may be other things ahead of this poison pill in the queue, and at this stage I would like the queue to stop right away.