Multiple Notify Icons in the System Tray in Winforms
Asked Answered
U

2

8

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.

Unfortunate answered 4/11, 2013 at 17:12 Comment(1)
Have you solved this? Please tell me how you did it because I couldn't do it yet. Thanks.Electromechanical
P
1

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.

Poesy answered 4/11, 2013 at 17:28 Comment(4)
Application.Exit(0) shows at Invalid Arguments, Cant we give Application.Exit();Unfortunate
Moreover the method you provided is for the Closing event. But even though i dont close the application. Still when i perform mutiple times minimize and back to normal state i can find many icons in the system tray. How do i deal with that?Unfortunate
Yeah thanks. But i figured it out that there was a typo mistake. So just went on with using Application.Exit();Unfortunate
I applied your idea of adding Application.Exit() but it didnt workout, I can still see multiple Icons in the System TrayUnfortunate
E
0

Icons only remain in notification area and disappear on mouse hover because the application is not exiting cleanly. Are you exiting the application or stopping the debug in VS? This also happens when an exception is thrown and app exists abruptly.

Empery answered 4/11, 2013 at 17:22 Comment(1)
I'm infact exiting the Application. Is there a way to deal with this problem.?Unfortunate

© 2022 - 2024 — McMap. All rights reserved.