Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Notify Icons in the System Tray in Winforms

Probably my question is duplicate of this Multiple icons open in tray bar. In my winforms application I'm showing the Application in the system tray once the form is closed ie the application doesnt exit after closing the form but exits on clicking "Close" on the Right Click Context menu on the system tray of the Applicaion.

But as I go on with using the application I notice that there are many more Notification Icon's in the system tray. But once I mouse hover over them they all disappear except the one with the application running. I've tried every method to eliminate the multiple icon's but I'm not able to do so.

Below is my code For Minimizing to 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 the form load I have 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();
        }

On the mouse click of the Icon I have written 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 Clearing the Notify Icons, but even that didn't help me. Am I missing something really obvious. Any help would be appreciated.

like image 997
Vikneshwar Avatar asked Nov 04 '13 17:11

Vikneshwar


1 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 do not need this.Dispose as it will be called in Application.Exit()

Check if the process is still running in task manager if it is end it and see if the icon disappears.

like image 68
06needhamt Avatar answered Oct 01 '22 12:10

06needhamt