locked
SerialPort Exceptions For My Class RRS feed

  • Question

  • I'm writing a communication protocol Class that uses the SerialPort.  I would like to know how to get exceptions that are thrown within the Class to throw within the WPF app that uses it.  Meaning...

    The communication Class is setup like so...

    private SerialPort _serialPort;
    
        public MyComm()
        {
          _serialPort = new SerialPort("COM1");
          _serialPort.ReceivedBytesThreshold = 1;
          _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
        }
    
        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          throw new NotImplementedException("You Received Some Data.");
        }

     

    In my WPF app, I have the following code...

    public MyComm _myComm;
    
    
        public Form1()
        {
          InitializeComponent();
    
          _myComm = new MyComm();
    
        }
    

     

    I  would like to know how to throw the event from my WPF app when the DataReceived event is thrown.  Any help is appreciated.

     

    Tuesday, December 14, 2010 10:33 PM

Answers

  •  

    Hi  shaggygi,

     

    Welcome to MSDN Forums!

     

    I think the code in your post is a windows Form application not a WPF application.

     

    If you want catch this exception in WPF, then you can use the Application.DispatcherUnhandledException Event in your application.

    Since I'm not a WPF expert, I think the following link will take you to read to expert information to help you use this event in your WPF application.

    http://www.srtsolutions.com/dealing-with-unhandled-exceptions-in-wpf

     

     

    If you want to know more about WPF, then you can create a new thread in the following forum:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/threads

     

     

    If you want to catch this exception in winForm, then you can use the AppDomain.UnhandledException Event in your application.

    First of all, we need to assign a handler to handle the Application.ThreadException event for the former, the AppDomain.CurrentDomain.UnhandledException event for the latter, and set Application.SetUnhandledExceptionMode to UnhandledExceptionMode.CatchException to force all Windows Forms errors to go through our handler, regardless of the settings in the application's user configuration file. UnhandledExDlg does it in its default constructor:

     

    Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction); 
    

     

    This is a sample code will let you how this work in the winForm application, you also can learn it easier.

    http://www.codeproject.com/KB/exception/UnhandledExceptionClass.aspx

     

     

    We also have a special forum for discuss winForm question, so you can create thread there if you want know more about winForm:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/threads

     

    If you have any concern, please feel free to let us know.

     

    Have a nice day!

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
     

    • Marked as answer by Mike Dos Zhang Monday, December 27, 2010 5:28 AM
    Friday, December 17, 2010 9:35 AM

All replies

  • Subscribe to the event, but remember it's not on the UI thread.
    Tuesday, December 14, 2010 10:55 PM
  •  

    Hi  shaggygi,

     

    Welcome to MSDN Forums!

     

    I think the code in your post is a windows Form application not a WPF application.

     

    If you want catch this exception in WPF, then you can use the Application.DispatcherUnhandledException Event in your application.

    Since I'm not a WPF expert, I think the following link will take you to read to expert information to help you use this event in your WPF application.

    http://www.srtsolutions.com/dealing-with-unhandled-exceptions-in-wpf

     

     

    If you want to know more about WPF, then you can create a new thread in the following forum:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/threads

     

     

    If you want to catch this exception in winForm, then you can use the AppDomain.UnhandledException Event in your application.

    First of all, we need to assign a handler to handle the Application.ThreadException event for the former, the AppDomain.CurrentDomain.UnhandledException event for the latter, and set Application.SetUnhandledExceptionMode to UnhandledExceptionMode.CatchException to force all Windows Forms errors to go through our handler, regardless of the settings in the application's user configuration file. UnhandledExDlg does it in its default constructor:

     

    Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction); 
    

     

    This is a sample code will let you how this work in the winForm application, you also can learn it easier.

    http://www.codeproject.com/KB/exception/UnhandledExceptionClass.aspx

     

     

    We also have a special forum for discuss winForm question, so you can create thread there if you want know more about winForm:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/threads

     

    If you have any concern, please feel free to let us know.

     

    Have a nice day!

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
     

    • Marked as answer by Mike Dos Zhang Monday, December 27, 2010 5:28 AM
    Friday, December 17, 2010 9:35 AM
  •  

    Hi  shaggygi,

     

    Have you tried the suggestions?

    And has this problem been solved?

     

    If there's any concern, please feel free to let me know.

     

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 21, 2010 9:06 AM