Multiple system tray notification icons in Winforms

Perhaps my question is duplicating this. Several icons open in the tray panel . In my winforms application, I show the application in the system tray after closing the form, i.e. The application does not exit after closing the form, but exits when you click "Close" in the "Right Click" context menu on the Applicaion taskbar.

But when I continue to use the application, I notice that there are many notification icons in the system tray. But as soon as the mouse runs over them, they all disappear, except for those that have the application running. I tried each method to eliminate the multiple icon, but I can not do this.

Below is my code To minimize the system tray

public void MinimizeToTray() { try { this.WindowState = FormWindowState.Minimized; TrayIcon.Visible = true; TrayIcon.ShowBalloonTip(1000); ShowInTaskbar = false; //this.Activate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

On loading the form, I added this code

 private void LoadTrayMenu() { TrayMenu.Items.Add("Reminder"); TrayMenu.Items.Add("Close"); TrayMenu.Items[0].Click += new EventHandler(this.Reminder_Click); TrayMenu.Items[1].Click += new System.EventHandler(this.Dispose_Click); TrayIcon.ContextMenuStrip = TrayMenu; } 

The dispose event is as follows

 private void Dispose_Click(object Sender, EventArgs e) { TrayIcon.Visible = false; TrayIcon.Icon = null; TrayIcon.Dispose(); this.Dispose(); } 

When I clicked the icon, I wrote the following code

 private void TrayIcon_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { this.Show(); this.WindowState = FormWindowState.Normal; TrayIcon.Visible = false; //TrayIcon.Icon = null; //TrayIcon.Dispose(); ShowInTaskbar = true; } } 

I tried to clear the notification icons, but even that didn't help me. I missed something really obvious. Any help would be appreciated.

+6
source share
2 answers

Add Application.Exit() to this method here

 private void Dispose_Click(object Sender, EventArgs e) { TrayIcon.Visible = false; TrayIcon.Icon = null; TrayIcon.Dispose(); Application.Exit() } 

you don't need this.Dispose since it will be called in Application.Exit()

Make sure that the process is still running in the task manager if it terminates it and sees if the icon disappears.

+1
source

Icons remain only in the notification area and disappear when you hover over the mouse because the application does not crash. Are you leaving the application or stop debugging in VS? This also happens when an exception is thrown and the application exists suddenly.

0
source

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


All Articles