Today I played with my project and found an interesting small fragment, given the following template, you can safely clear the stream, even if it forced to close earlier. My project is a network server on which a new thread is created for each client. I found this useful for early termination from the remote side, but also from the local side (I can just call .Abort()inside my processing code).
Are there any problems you can see with this, or any suggestions you would make to anyone looking for a similar approach?
The following is a test case:
using System;
using System.Threading;
class Program
{
static Thread t1 = new Thread(thread1);
static Thread t2 = new Thread(thread2);
public static void Main(string[] args)
{
t1.Start();
t2.Start();
t1.Join();
}
public static void thread1() {
try {
while(true) {
Thread.Sleep(100);
}
} finally {
Console.WriteLine("We're exiting thread1 cleanly.\n");
}
}
public static void thread2() {
Thread.Sleep(500);
t1.Abort();
}
}
For reference, without a try / finally block, the thread just dies, as you might expect.