IOException我无法捕获

Nic*_*ck3 6 c# serial-port ioexception

我有一个应用程序与USB-GPS交谈.它是一种魅力,如果没有任何不寻常的幸福.但我有一个大问题.如果USB被拔出,我的程序(有时)会崩溃.我有Try/Catch我需要它们但是这个IOExeption没有被捕获.我只是得到"设备无法识别命令",程序停止.以下是启动端口的代码:

        public LatLongFromGPS(Form1 parent)
    {
        this.parent = parent;
        String port;
        this.SPort = new SerialPort(port, 4800);
        this.SPort.ReadTimeout = 500;
        this.SPort.DataReceived += new SerialDataReceivedEventHandler(dataReceived);
    }

    public bool checkIfPortsOpen()
    {
        return (this.SPort.IsOpen);
    }

    public void openPort()
    {
        try
        {
            if (!this.SPort.IsOpen)
            {
                this.SPort.Open();
            }
        }
        catch(Exception ex)
        {
            parent.LoggIt.WriteLogg("OPENPORT " + ex.ToString(), Logger.LoggType.Debug);
        }
    }

    public void dataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            if (SPort.IsOpen)
            {
                String GPGGAString;
                Thread.CurrentThread.Join(200);
                buffert = new char[this.SPort.BytesToRead];
                this.SPort.Read(buffert, 0, buffert.Length);
                GPGGAString = findStringFromGPS();
                if (GPGGAString != null)
                {
                    getLatitudefromString(GPGGAString);
                    getLongitudefromString(GPGGAString);
                    getTimeFromString(GPGGAString);
                    this.newData = true;
                }
            }
        }
        catch(Exception ex)
        {
            parent.LoggIt.WriteLogg("GPSERROR    " + ex.ToString(), Logger.LoggType.Debug);
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我在计时器中检查信息

if (this.LatLong.newDataReceived())
   {
        //DOING STUFF
   }

if (!this.LatLong.checkIfPortsOpen())
       this.LatLong.openPort();
Run Code Online (Sandbox Code Playgroud)

任何人有任何建议如何阻止崩溃?

[编辑]堆栈:

at System.IO.Ports.InternalResources.WinIOError(Int32, System.String)

at System.IO.Ports.InternalResources.WinIOError()

at System.IO.Ports.SerialStream.Dispose(Boolean)

at System.IO.Ports.SerialStream.Finalize()
Run Code Online (Sandbox Code Playgroud)

NDJ*_*NDJ 1

我不完全确定它是否适用于此,但有一些机制可以捕获应用程序域级别的整体崩溃 - http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx

(不是有关其他事件的部分,例如 ThreadException - 根据情况,这些事件可能需要自己的处理程序)