Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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?
Posted
Updated 15-Apr-12 21:06pm
v2

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900