0plus1
0plus1

Reputation: 4555

Can't close C# application

I'm developing a C# application and when the user clicks on the X, the application gets minimized inside a trayicon. Like so:

private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    Hide();
}

The application is really simple (only one form). The problem is that I can't manage to properly close the application. When the user rights clicks on the tray icon and he chooses "exit" he should be able to close the application. The problem is that even if the tray icon gets unloaded and the form is closed, the application still shows in the Task Manager as an active application. I'm closing the application like this:

private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
{
    trayIcon.Dispose();
    this.Close();
    Application.Exit();
}

What am I missing here?

Upvotes: 1

Views: 3010

Answers (8)

Ben Dover
Ben Dover

Reputation: 1

Everyone else is over doing it.

Just do Close();

Upvotes: 0

Suraj Azad
Suraj Azad

Reputation: 1

I have tried Environment.Exit(0). It works in this case, and it worked fine for me.

Upvotes: 0

Andez
Andez

Reputation: 5858

I did something similar a while back.

You need to know what is causing the form to close. So when you click on the X, there is a specific reason passed to the FormClosing event. Like so:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    // don't close just yet if we click on x
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        this.Hide();
    }
}

Also, I have other code from the context menu Exit click:

private void tsmiExit_Click(object sender, EventArgs e)
{
    // close the application forefully
    TerminateApplication();
}

/// <summary>
/// Closes the Application.
/// </summary>
private void TerminateApplication()
{
    // need to forcefully dispose of notification icon
    this.notifyIcon1.Dispose();

    // and exit the application
    Application.Exit();
}

Edit:

Note: When you click on the X button, the close reason will be a CloseReason.UserClosing. When Application.Exit is called, the FormClosing is called again with a CloseReason.ApplicationExitCall.

End Edit:

Hope this helps

Andez

Upvotes: 5

David Heffernan
David Heffernan

Reputation: 613562

The e.Cancel = true line in frmChat_FormClosing is blocking the app from shutting down.

You can solve this easily enough by adding a boolean field to your form class, named TerminatingApp. Set this to true before calling this.Close(). Inside frmChat_FormClosing check for the value of TerminatingApp and only set e.Cancel = true if TerminatingApp is false.

Something like this:

private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!TerminatingApp)
    {
        e.Cancel = true;
        Hide();
    }
}

private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
{
    TerminatingApp = true;
    trayIcon.Dispose();
    this.Close();
    Application.Exit();
}

Upvotes: 2

Deepesh
Deepesh

Reputation: 5614

When you are calling "this.Close" its going to call "frmChat_FormClosing" and in that you are setting "e.Cancel = true" which is creating the problem, use some variable to identify from where close event is being called and set e.Cancel accordingly.

Upvotes: 0

DanielB
DanielB

Reputation: 20230

You could call Environment.Exit(0) where the parameter is the exit code.

Upvotes: 0

Dan Diplo
Dan Diplo

Reputation: 25359

This is where the documentation on Application.Exit() comes in handy (my emphasis in bold):

The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44595

Ciao, how does it work if you replace Application.Exit with Application.ExitThread ?

I am actually afraid that with your code, when you call this.Close you are getting into the previous method above with the cancelling...

Upvotes: 1

Related Questions