蓝牙USB断开连接时应用程序崩溃(使用串行端口类) [英] Application crashes (Using Serial Port class) on Bluetooth USB disconnection

查看:208
本文介绍了蓝牙USB断开连接时应用程序崩溃(使用串行端口类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从基本SerialPort类派生的以下CustomSerialPort类.

当我从PC/Notebok中删除Bluetooth USB设备时,我不知道为什么我仍然遇到串行端口崩溃/应用程序崩溃的异常.这是我正在使用的课程:

I am using the following CustomSerialPort class derived from base SerialPort class.

Don''t know why i am still getting the exception of Serial Port crashing / Application crashing when i am removing the Bluetooth USB device from my PC/Notebok. Here is the class i am using:

public partial class CustomSerialPort : SerialPort
    {
        public CustomSerialPort(string port)
            : base(port)
        {

        }
        public new bool Open()
        {
            try
            {
                base.Open();

                /*
                ** because of the issue with the FTDI USB serial device,
                ** the call to the stream's finalize is suppressed
                **
                ** it will be un-suppressed in Dispose if the stream
                ** is still good
                */
                GC.SuppressFinalize(BaseStream);
            }
            catch(Exception ex)
            {
                return false;
            }
            return true;
        }

        public new void Dispose()
        {
            Dispose(true);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (base.Container != null))
            {
                base.Container.Dispose();
            }

            try
            {
                var stream = (Stream)typeof(SerialPort).GetField("internalSerialStream", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);

                if (stream != null)
                {
                    try
                    {
                        stream.Dispose();
                    }
                    catch (Exception ex)
                    {
                    }
                }

                /*
                ** because of the issue with the FTDI USB serial device,
                ** the call to the stream's finalize is suppressed
                **
                ** an attempt to un-suppress the stream's finalize is made
                ** here, but if it fails, the exception is caught and
                ** ignored
                */
                GC.ReRegisterForFinalize(BaseStream);
            }
            catch(Exception ex)
            {
            }

            base.Dispose(disposing);
        }

        public new bool Close()
        {
            try
            {
                base.Close();
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
    }



有人可以建议我至少如何处理此类异常吗?



Can anyone suggest me how to handle at least such exceptions?

推荐答案

您将忽略所有异常,因此没有信息可帮助您诊断问题.这是非常糟糕的做法.发生异常时,至少您应该捕获并记录详细信息.
You are ignoring all your exceptions so there is no information to help you diagnose your problem(s). This is extremely bad practice. When an exception occurs, at the very least you should be capturing and logging the details.


这篇关于蓝牙USB断开连接时应用程序崩溃(使用串行端口类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆