Maximc
Maximc

Reputation: 1762

Window closing bug or e.cancel = false but it won t totaly shut down

After 2+ hours of looking WHY my bloody process would not exit when I closed the window. I finaly found out that it was the problem in the main window(and not in a thread, because that could have been also the problem)! But I still got NO idea why it bugs.

So THIS code makes it bug:

private void Window_Closing(object sender, CancelEventArgs e)
{
    try
    {
        MessageBox.Show(e.Cancel.ToString()); // False always

        if (formcontroler.formchampinfo != null) // THIS IS NULL so it won t go into this IF
        {
            formcontroler.formchampinfo.Close(); // never gets here so it doesn t matter what this do right ?
        }

        MessageBox.Show(e.Cancel.ToString()); // always false, just to check or it would get to this part tbh
    }
    catch (Exception ex)
    {
        throw (ex); // never gets thrown
    }
}

WHICH IS REALY Strange (to me)! because it gets to the 2nd messagebox and the e.cancel = FALSE so it should not cancel it and just shutdown and the process should get killed(and Visual studio should stop debugging).

Anyway it doesn t stop. It just keeps the process alife and I got no clue why, If I remove the middle if or replace it with a simpler one like:

private void Window_Closing(object sender, CancelEventArgs e)
{
    try
    {
        MessageBox.Show(e.Cancel.ToString()); // False always

        if (true == true) // just To test ofc
        {
            int lol = 5;
            lol++;
        }

        MessageBox.Show(e.Cancel.ToString()); // always false, just to check or it would get to this part tbh
    }
    catch (Exception ex)
    {
        throw (ex); // never gets thrown
    }
}

then it works and the program exits like it should(the process is killed and Visual studio stops debugging.

Some side code that shoudn t mattter I think

class formcontroler
{
    public static frmchampinfo formchampinfo;
}

The frmchampinfo is a window, but atm I loaded it or declaterded it (like formchampinfo = new frmchaminfo();) Is this a bug or what happened here ? I realy don t get why it doesn t shutdown totaly at my code.

SOLVED sorry I can t answer it until 7 hours but then I am asleep.(Because I don t got 100 rep yet)

Oke so after looking deeper and deeper I found out that the IF statement creates an other form in my formcontroller class(sorry I didn t provide full code in answer so you coudn t figure it out):

class formcontroler
        {
          public static frmchampinfo formchampinfo;
            public static Window activeform;
            public static frmlog formlog = new frmlog();
    //... more code blabla
        }

The formlog "gets made" here. If I add formcontroller.formlog.close() to the code then it can close completly.

 private void Window_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                MessageBox.Show(e.Cancel.ToString()); // False always
                if (formcontroler.formchampinfo != null) // THIS IS NULL so it won t go into this IF
                {
                    formcontroler.formchampinfo.Close(); // never gets here so it doesn t matter what this do right ?

                }
               formcontroler.formlog.Close(); // THIS FIXED IT... lame sorry for all your time. HoweveR I have learned something hope you did too.
                MessageBox.Show(e.Cancel.ToString()); // always false, just to check or it would get to this part tbh
            }
            catch (Exception ex)
            {
                throw (ex); // never gets thrown
            }
        }

Upvotes: 1

Views: 802

Answers (1)

thumbmunkeys
thumbmunkeys

Reputation: 20764

Check if the ShutdownMode of your App class is set to OnMainWindowClose.

You can also explicitly shutdown the application by:

 Application.Current.Shutdown();

Upvotes: 2

Related Questions