Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track crashing of window form?

Tags:

c#

winforms

How to track window form crashing ? Like any event is called or anything else is called or we can track that window form is crashed ? Like dispose is called window form is crashed. But anything else which is happened so we can track out crashing of window form ?

Like problem is I have one window application on that there is tutorial balloon on the main form which moves for each control on the main form and describes the application functionality by indicating control on the main form one by one. And each time balloon moves balloon is disposes and new balloon form is created.

Now I want to insert the step number in the database when that balloon was crashed. I cannot understand what should I do ? What is happened when that balloon window(window form) is crashed ? There is a dispose event which is occurred but it is happened each time balloon creates so is there any thing else to track crashing ?

EDIT: Sorry to all, I forgot to specify that it is with .net framework 2.0.

like image 334
Harikrishna Avatar asked Dec 07 '25 20:12

Harikrishna


2 Answers

Use this: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

If any undhandled exception occurs in a forms thread, it will get here. If it's null, you get the usual dialog (undhandled exception occurred, you can continue or close, and see the stack trace).

like image 145
fejesjoco Avatar answered Dec 10 '25 11:12

fejesjoco


This is an excerpt from a small Windows Forms 2.0 program of mine:

[STAThread]
private static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.ThreadException +=
        applicationThreadException;

    // Set the unhandled exception mode to force all Windows Forms 
    // errors to go through our handler.
    Application.SetUnhandledExceptionMode(
        UnhandledExceptionMode.CatchException);

    AppDomain.CurrentDomain.UnhandledException +=
        currentDomainUnhandledException;

    ...
}

With the two handlers

private static void currentDomainUnhandledException(
    object sender,
    UnhandledExceptionEventArgs e)
{
    handleException(e.ExceptionObject as Exception);
}

and

private static void applicationThreadException(
    object sender,
    ThreadExceptionEventArgs e)
{
    handleException(e.Exception);
}

The actual function to handle the exceptions does in my example:

private static void handleException(
    Exception exception)
{
    LogCentral.Current.LogError(
        @"Exception occurred.",
        exception);

    if (ErrorForm.IsErrorFormShowing)
    {
        LogCentral.Current.LogInfo(
            @"Error form already showing, not showing again.",
            exception);
    }
    else
    {
        using (var form = new ErrorForm(exception))
        {
            var result = form.ShowDialog();

            if (result == DialogResult.Abort)
            {
                Application.Exit();
            }
        }
    }
}

I.e. it logs the error via log4net and then displays an error form to show the user further information (exception message) and allow to quit the application.

like image 40
Uwe Keim Avatar answered Dec 10 '25 10:12

Uwe Keim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!