Process crash when using SerialPort or Socket objects and thread.Abort
The easiest way to demonstrate what this error is all about is to compile and run this code: using System; namespace ST public static void ReadFromPort() public static void Main() Thread thread = new Thread(new ThreadStart(ReadFromPort)); Console.WriteLine("Starting (read)thread."); Thread.Sleep(500); try Thread.Sleep(500); Console.WriteLine("Not here."); Pay attention on " thread.Abort();" call. The "m_port.Read" has a large timeout and during this rading (that is executin in another thread) we call Abort on that thread. Serial port returns an exception, but that exception cannot be captured and the process is killed. Seems like the problem is in implementation of the SerialPort class. But it seems to work fine if we close the port before we call thread.Abort. Then the exception can be captured. Can this issue be resolved so that the thrown exception can be captured? Without closing the serial port before aborting the thread. Thanks.
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;
{
public class SerialTest
{
public static SerialPort m_port;
{
byte []buffer=new byte[10];
try
{
m_port.Read(buffer, 0, 10);
}
catch (Exception e)
{
Console.WriteLine("Read thread exception: "+e.ToString());
}
}
{
m_port = new SerialPort("COM1");
m_port.Open();
m_port.ReadTimeout = 100000;
thread.Start();
{
Console.WriteLine("Thread Abort.");
thread.Abort();
}
catch (Exception e)
{
Console.WriteLine("Main thread exception: "+e.ToString());
}
Console.ReadLine();
}
}
}

