Problem with SerialPort

后端 未结 6 674
自闭症患者
自闭症患者 2020-12-15 00:08

I\'m working with SerialPort to communicate (read only) with barcode reader.

I\'ve installed driver to operate with the reader as if it was connected via Com-port,

6条回答
  •  醉梦人生
    2020-12-15 00:57

    You can inherit from SerialPort and override the Dispose() method to handle such exceptions. You can just gobble the exception (Dispose shouldn't be throwing anyway).

    If you want to log the exception or handle it in some other way, you will have to check the disposing flag first. If it is false it means that Dispose was called by SerialPort's destructor and the object is already orphaned.

    E.g.

        public class MySerialPort:SerialPort
        {
            protected override void Dispose(bool disposing)
            {
                try
                {
                    base.Dispose(disposing);
                }
                catch (Exception exc )
                {                    
                    if (disposing)
                    {
                        //Log the error
                    }
                }
    
            }
        }
    

提交回复
热议问题