How to track window form failure?

How to track window form failure? What is the name of any event, or is something else triggered, or can we track that the window form has crashed? Like dispose is called a window form. But is there anything else that happened so that we can track the window form crash?

Like the problem, I have one window application on which there is a main pool on the main form, which moves for each control in the main form and describes the functionality of the application, pointing to the main form one by one. And every time the balloon moves, the balloon opens and a new balloon shape is created.

Now I want to insert the step number into the database when this balloon was broken. I don’t understand what should I do? What happened when this ball window (window shape) crashed? There is a dispose event that happens, but it happens every time a balloon is created, so is there anything else to track for the crash?

EDIT: Sorry, I forgot to indicate that this is with the .net framework 2.0.

+3
source share
3 answers

In the Program.cs file, place the try / catch block inside the Main () function. The idea is to have Application.Run (yourformhere) inside this block. Then, inside the catch, you can probably save some state (for example, the step at which the ball broke the shape) in the database. Good luck

+1
source

This is an excerpt from my small Windows Forms 2.0 program:

[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 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);
}

Actual function to handle exceptions 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();
            }
        }
    }
}

those. it logs the error with log4net , and then displays the error form to show additional user information (exception message) and allow the user to exit the application.

+2
source

Source: https://habr.com/ru/post/1783206/


All Articles