NotifyIcon remains in Tray even after application

2019-01-22 10:35发布

There are many questions on SO asking same doubt. Solution for this is to set

notifyIcon.icon = null and calling Dispose for it in FormClosing event.

In my application, there is no such form but has Notification icon which updates on Events. On creation, I hide my form and make ShowInTaskbar property false. Hence I can not have a "FormClosing" or "FormClosed" events.

If this application gets event to exit, It calls Process.GetCurrentProcess().Kill(); to exit.

I have added notifyIcon.icon = null as well as Dispose before killing, but still icon remains taskbar until I hover mouse over it.

EDIT: If I assume that this behaviour is due to calling GetCurrentProcess().Kill(), Is there any elegant way to exit from application which will clear all resources and remove icon from system tray.

标签: c# notifyicon
13条回答
相关推荐>>
2楼-- · 2019-01-22 11:08

The right answer has already been given. But you must also provide a delay, for example with a timer. Only then the application can still remove the icon in the background.

private System.Windows.Forms.Timer mCloseAppTimer;
private void ExitButton_Click(object sender, EventArgs e) 
{ 
    notifyIcon.Visible = false; notifyIcon.Dispose; 
    mCloseAppTimer = new System.Windows.Forms.Timer(); 
    mCloseAppTimer.Interval = 100; 
    mCloseAppTimer.Tick += new EventHandler(OnCloseAppTimerTick); 
} 
private void OnCloseAppTimerTick(object sender, EventArgs e) 
{ 
    Environment.Exit(0); // other exit codes are also possible 
}
查看更多
叛逆
3楼-- · 2019-01-22 11:10

You can either set

notifyIcon1.Visible = false;

OR

notifyIcon.Icon = null;

in the form closing event.

查看更多
冷血范
4楼-- · 2019-01-22 11:11

Use notifyIcon.Visible = False in FormClosing event

查看更多
叛逆
5楼-- · 2019-01-22 11:14

I had the exact same problem as you.

The proper way are send WM_CLOSE message to a process.
I use the c# code I found in this article.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way

查看更多
成全新的幸福
6楼-- · 2019-01-22 11:15

Components just must be disposed in the right order like this :

NotifyIcon.Icon.Dispose();

NotifyIcon.Dispose();

Add this to the MainWindow closing event.

Hope this will help.

查看更多
我只想做你的唯一
7楼-- · 2019-01-22 11:18

I tried this code. I thing this is easier. I will write it for several tools or events. I hope it will be helpful for you.

Use this code when you want to do it when you press the Exit or Close button:

    private void ExitButton_Click(object sender, EventArgs e)
    {
            notifyIcon.Dispose;
            Application.Exit();        //   or   this.Close();
    }

Use this code when you want to do it when form is closing:

    private void Form1_FormClosing(object sender, EventArgs e)
    {
            notifyIcon.Dispose;
            Application.Exit();        //   or   this.Close();
    }

The important code is this:

    notifyIcon.Dispose;
查看更多
登录 后发表回答