开发者

Can't close C# application

开发者 https://www.devze.com 2023-03-10 11:44 出处:网络
I\'m developing a C# application and when the user 开发者_如何学JAVAclicks on the X, the application gets minimized inside a trayicon. Like so:

I'm developing a C# application and when the user 开发者_如何学JAVAclicks 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?


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


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


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...


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.


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


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.


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


Everyone else is over doing it.

Just do Close();

0

精彩评论

暂无评论...
验证码 换一张
取 消