I've written an application with a plug-in feature, and if a plug in doesn't respond quickly to a stop request, I call Thread.Abort()
on its worker thread. This seems to work most of the time, but I recently discovered that it throws an ObjectDisposedException
if the thread is aborted while it's writing to the serial port. With some USB-to-serial drivers in Windows XP, it even causes a blue screen of death.
I think I can use a producer/consumer pattern to make it so that the worker thread doesn't directly write to the serial port, but I'd like to know if there's any more detail available about why the serial port freaks out.
Here's a simplified example that reproduces the problem:
using System;
using System.IO.Ports;
using System.Threading;
class Program
{
private static SerialPort port = new SerialPort();
static void Main(string[] args)
{
port.PortName = "COM1";
port.Open();
for (int i = 0; i < 10; i++)
{
var workerThread = new Thread(Loop);
workerThread.Start();
Thread.Sleep(1000);
workerThread.Abort();
Thread.Sleep(1000);
Console.Out.WriteLine("Finished {0}.", i);
}
}
static void Loop()
{
for (int i = 0; i < 1000000; i++)
{
port.Write(new byte[] {0, 0, 0, 0, 0, (byte)(i % 256)}, 0, 6);
}
}
}