Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm fairly new to C# and I was curious, is there an event I can call when an application crashes? I have a program that writes and formats .csv files for use in store inventory, but some of these computers are riddled with errors (Not to mention my own noviceness with C#) are causing a few crashes, which causes people to lose work. So, I wanted to write an event to be called upon crash that saves a copy of their current work to their My Documents folder. If there is no event that does this, could you please give me a way to sorta go about doing this? Thanks!
Posted

You can see this link->

Click
 
Share this answer
 
Comments
Johnny J. 19-Oct-10 11:53am    
Good link
I'm guessing this is a Winforms application?

You could set up some 'global' error handlers that will trap any exceptions you haven't explicitly handled in your code.

In your application startup, have some code along the lines of

C#
/// <summary>
/// Main thread exception handler
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">event</param>
public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{
}

/// <summary>
/// Application domain exception handler
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">event</param>
public static void AppDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
{
}

[STAThread]
public static void Main()
{
    // Unhandled exceptions for our Application Domain
    AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(AppDomain_UnhandledException);

    // Unhandled exceptions for the executing UI thread
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

    // Whatever else your application does on startup

}


In the event stubs, write some code that will either log the exception details or do whatever

Some exceptions can be recovered from, but some will cause an application crash....best get debugging and find out what is causing the problem!
 
Share this answer
 
There is no real event for an application crash (other than events mentioned in some of the other answers here) - when an application crashes, it has stopped working, so all further execution stops.

You need to use a tool like WinDbg or any other debugger in such cases.
 
Share this answer
 
v3
Comments
Johnny J. 19-Oct-10 12:05pm    
Small comment, Abhinav: I don't want to be rude, but in my humble opinion, it's not good to write something like "the two mentioned above"... The order of the answers can/will change based on the voting, so "the two above" might be "the two below" in a minute...
Abhinav S 19-Oct-10 12:16pm    
Ah I see. Good point. Will fix it...Thank you.
I think you need some knowledge about exception handling, better than an event. Anyway, I will give you two links, so you can decide which one gives you a better solution:

1. Exception handling

2. Application.ThreadException event

Hope this helps. See you
 
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