locked
Catch exception in endinvoke callback RRS feed

  • Question

  • Hi,

    I have an asynchronous event :

    public event EventHandler MessageReceived;
    protected void OnMessageReceived(EventArgs eaArgs)
    {
        if(MessageReceived != null)
           // Raises the event asynchronously
           MessageReceived.BeginInvoke(this, eaArgs, AsyncEndEventCallBack, MessageReceived);
    }

    with this endinvoke callback:

    private void AsyncEndEventCallBack(IAsyncResult iarResult)
    {
    	try
    	{
    
    		// Retrieve the delegate.
    		AsyncResult result = (AsyncResult)iarResult;
    		EventHandler caller = (EventHandler)result.AsyncDelegate;
    		caller.EndInvoke(iarResult);
    	}
    	catch(Exception e)
    	{
    		// TODO : throw the exception to the main program
    	}
    
    }

    I catch the exception of the event in the call back method. I want thrown this exception to the main program :

    [STAThread]
    static void Main()
    {
    	try
    	{
    		// Set the application style
    		Application.EnableVisualStyles();
    		Application.SetCompatibleTextRenderingDefault(false);
    
    		// Create the main window
    		Main mMain = new Main();
    		// Load the plugin interface
    		mMain.LoadInterface();
    		// Load the configuration
    		mMain.LoadConfiguration();
    		// Show the main window
    		mMain.Show();
    		Application.Run(mMain);
    	}
    	catch(Exception eException)
          {
    		// Display a message
    		ErrorMessage.Show(eException);
    				
          }
    		
    }
    How to throw the exception catched in the callback to the main program?
    Friday, June 25, 2010 8:32 AM

Answers

  • Another way to do this is to call EndInvoke() from the main UI thread at some point. When you do that, any exception that was thrown in the invoked thread will be rethrown in the main UI thread.

    The issue here is knowing when to call EndInvoke() from the main UI thread... This usually means implementing some other form of interthread communcation such as a ManualResetEvent that gets set when the thread is done. Or you can poll the IAsyncResult value returned from BeginInvoke(). 

    See here for more details: http://support.microsoft.com/kb/315582

    • Marked as answer by Troopers Friday, June 25, 2010 12:22 PM
    Friday, June 25, 2010 11:54 AM

All replies

  • Welcome to the MSDN Forums.

    Like below using throw statement:

    private void AsyncEndEventCallBack(IAsyncResult iarResult)
    {
    	try
    	{
    
    		// Retrieve the delegate.
    		AsyncResult result = (AsyncResult)iarResult;
    		EventHandler caller = (EventHandler)result.AsyncDelegate;
    		caller.EndInvoke(iarResult);
    	}
    	catch(Exception e)
    	{
    		// TODO : throw the exception to the main program
                throw e;
    	}
    
    }
    

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 25, 2010 8:53 AM
  • Normally when you throw the exception from the sub-thread and you want to catch in the main thread (the parent caller) - you can not. 

    to handle that, you need to register the 'UnhandledException' event of the 'AppDomain.CurrentDomain' class in the main method of the program. this event will handle all the unhandled exception of the program and the exception thrown by sub-thread.

    see below example:

     

    static void Main(string[] args)
      {
       
       AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    }
    
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
      {
       throw new NotImplementedException();
      }

     

    Hope this will help...


    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. My Blog: http://ashishkhandelwal.arkutil.com
    Friday, June 25, 2010 8:59 AM
  • throw e; don't work because we are in a sub thread
    Friday, June 25, 2010 9:02 AM
  • I have see this handle but i search a solution more smart.

    Friday, June 25, 2010 9:05 AM
  • As far as i know, there is no other way (apart from given above) to throw the exception from Sub-Thread to the main thread. however there could be some other work around like setting a flag at sub-thread to inform main thread about the exception and so on.
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. My Blog: http://ashishkhandelwal.arkutil.com
    Friday, June 25, 2010 9:14 AM
  • throw e; don't work because we are in a sub thread


    It works but I think you mean that you can't catch it from the thread!

    For catching these exceptions, I think you can use Application.ThreadException according to it's documentation at http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx (Application.ThreadException Event).

    Hope this helps.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    Friday, June 25, 2010 9:35 AM
  • Another way to do this is to call EndInvoke() from the main UI thread at some point. When you do that, any exception that was thrown in the invoked thread will be rethrown in the main UI thread.

    The issue here is knowing when to call EndInvoke() from the main UI thread... This usually means implementing some other form of interthread communcation such as a ManualResetEvent that gets set when the thread is done. Or you can poll the IAsyncResult value returned from BeginInvoke(). 

    See here for more details: http://support.microsoft.com/kb/315582

    • Marked as answer by Troopers Friday, June 25, 2010 12:22 PM
    Friday, June 25, 2010 11:54 AM