none
Handling unhandled exception RRS feed

  • Question

  • I have a .Net 4.0 C# service that has a few threads that are created.

    After running on a certain amount of data, the service stops.  I can see in the windows event log that it has crashed.

    Since I am not able to catch whatever exception is being thrown, I want to create an event handler to catch unhandled exceptions. 

    I am not sure how to go about creating this event handler and tying it to the threads I have.

    Can someone please give me some guidance on how to do this?

    Thank you.

    Thursday, January 17, 2013 8:54 PM

Answers

  • So looking at these links the easiest way is to do something like this...

    protected override void Onstart()
    {
       AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    }
    
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
      Exception e = (Exception)args.ExceptionObject;
    }
    


    This would allow me to catch all unhandled exceptions from various threads correct?

    I am trying it out now.

    Friday, January 18, 2013 1:06 AM

All replies

  • Check these links:

    http://stackoverflow.com/questions/4191930/exception-handling-in-the-multi-thread-environment

    http://msdn.microsoft.com/en-us/library/dd997415(v=vs.100).aspx

    Thursday, January 17, 2013 8:57 PM
  • This stackoverflow question has a pretty good answer to the problem you're looking to solve.

    Matt

    Thursday, January 17, 2013 8:59 PM
  • So looking at these links the easiest way is to do something like this...

    protected override void Onstart()
    {
       AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    }
    
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
      Exception e = (Exception)args.ExceptionObject;
    }
    


    This would allow me to catch all unhandled exceptions from various threads correct?

    I am trying it out now.

    Friday, January 18, 2013 1:06 AM
  • Hi there,

    Yes, it is.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 18, 2013 5:15 PM
    Moderator
  • So no exceptions were found and the Service is still crashing.

    Any other suggestions?  

    I am going to try using the  HandleProcessCorruptedStateExceptionsAttribute  on some of the methods where the main work is being done to see what happens.

    Sunday, January 20, 2013 3:28 PM
  • You said the service stops. And then you said it's crashing. It's an important distinction. What does the event log say? There should be some indication of the service stopping.
    Monday, January 21, 2013 11:31 PM
  • Sorry for the confusion.  The service crashes.

    The Application log tells me I get an Application Error with the exception code 0xc0000008.

    The next event in the log right after is a Windows Error reporting telling me that the event Name is APPCRASH.
    Wednesday, January 23, 2013 12:40 AM
  • No luck still.  I still have no idea why my service is crashing.
    Wednesday, January 23, 2013 12:44 AM
  • Your event log entry should have more information than that including a faulting module. Provide the whole text so we can look at it.

    You should get a memory dump of the process when its close to crashing. Monitor for CPU usage and handle count and you can get a metric to use with a tool like procdump to do an automated dump.

    Wednesday, January 23, 2013 1:55 AM
  • Error Event 1

    Faulting application name: Ex.exe, version: 1.0.0.0, time stamp: 0x50fdc416
    Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
    Exception code: 0xc0000008
    Fault offset: 0x77c2fd37
    Faulting process id: 0xc8c
    Faulting application start time: 0x01cdf8289700f3e6
    Faulting application path: C:\Users\ch\Documents\Visual Studio 2010\Projects\AMP\src\E\bin\Debug\Ex.exe
    Faulting module path: unknown
    Report Id: e8b7a4d8-64ac-11e2-9803-005056c00008

    Information Event

    Fault bucket , type 0
    Event Name: APPCRASH
    Response: Not available
    Cab Id: 0

    Problem signature:
    P1: Ex.exe
    P2: 1.0.0.0
    P3: 50fdc416
    P4: StackHash_0721
    P5: 0.0.0.0
    P6: 00000000
    P7: c0000008
    P8: 77c2fd37
    P9:
    P10:
    Wednesday, January 23, 2013 11:39 PM
  • Does this problem occur if you run a Release build? I see you're pointing at a Debug build from the event message.

    Thursday, January 24, 2013 12:13 AM
  • I will try.  All my development and testing so far has been in Debug.

    Thursday, January 24, 2013 1:26 AM
  • By the way.  I ran Procdump and Procmon.

    Procdump did not create a dump around the time of the crash, so I will have to run it again.

    However, Procmon indicated a buffer overflow somewhere close to the time the service crashed.  However, I was not able to tell exactly where.

    Does that give any more insight?

    Thanks.

    Thursday, January 24, 2013 2:46 PM