Swanand
Swanand

Reputation: 4115

NotifyIcon remains in Tray even after application closing but disappears on Mouse Hover

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.

Upvotes: 92

Views: 67707

Answers (15)

BitWiseByteDumb
BitWiseByteDumb

Reputation: 377

I couldn't make any one of the other solutions work. It turned out to be kind of a hybrid of all the above! I hunted and pecked until I had a consistently working solution both in debug and in EXE execution modes! I have no idea why MSFT would mark this as "Not going to fix"? I wish I had that liberty!

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //THIS CODE IS CRAZY BUT MUST BE DONE IN ORDER TO PROPERLY REMOVE THE SYSTEM TRAY ICON!
        //GREAT ARTICLE ON THIS ON STACK OVERFLOW
        //https://stackoverflow.com/questions/14723843/notifyicon-remains-in-tray-even-after-application-closing-but-disappears-on-mous

        //BTW THIS IS KIND OF A BLEND OF ALL THE SOLUTIONS BECAUSE I COULD NOT FIND A SINGLE SOLUTION THAT WOULD WORK!
        systray_icon.Visible = false;
        while (systray_icon.Visible)
        {
            Application.DoEvents();
        }
        systray_icon.Icon.Dispose();
        systray_icon.Dispose();
        Environment.Exit(1);
    }

Upvotes: 0

Rafael
Rafael

Reputation: 41

The only way that works to me was:

  1. On design screen changing notifyicon1 property visible=false

  2. Insert the code below on main form "activated" event:

NotifyIcon1.Visible = True
  1. Insert the code below on main form "closing" event:
NotifyIcon1.Visible = false
NotifyIcon1.Icon.Dispose()
NotifyIcon1.Dispose()

Upvotes: 4

Pouya
Pouya

Reputation: 192

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

Upvotes: 12

Terrence Meehan
Terrence Meehan

Reputation: 21

I tried all of these and none of them worked for me. After thinking about it for a while I realized that the application creating the "balloon" was exiting before it had a chance to actually dispose of the balloon. I added a while loop just before Application.Exit() containing an Application.DoEvents() command. This allowed my NotifyIcon1_BalloonTipClosed to actually finish disposing of the icon before exiting.

while (notifyIcon1.Visible)
{
    Application.DoEvents();
}
Application.Exit();

And the tip closed method: (You need to include the thisIcon.visible = false in order for this to work)

private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
    var thisIcon = (NotifyIcon)sender;
    thisIcon.Icon = null;
    thisIcon.Visible = false;
    thisIcon.Dispose();
}

Upvotes: 1

Ismael
Ismael

Reputation: 357

Use notifyIcon.Visible = False in FormClosing event

Upvotes: 11

Shim-Sao
Shim-Sao

Reputation: 2126

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.

Upvotes: 18

Can DOGRU
Can DOGRU

Reputation: 69

edit codes of ...Designer.cs as below coding.

        protected override void Dispose(bool disposing)
           {
           if (disposing )
               {
               this.notifyicon.Dispose();
               }
           base.Dispose(disposing);
           }

Upvotes: 0

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 
}

Upvotes: -2

The Muffin Man
The Muffin Man

Reputation: 20004

The only solution that worked for me was to use the Closed event and hide and dispose of the icon.

icon.BalloonTipClosed += (sender, e) => { 
                                            var thisIcon = (NotifyIcon)sender;
                                            thisIcon.Visible = false;
                                            thisIcon.Dispose(); 
                                        };

Upvotes: 22

Cubi73
Cubi73

Reputation: 1951

Try Application.DoEvents(); after setting notifyIcon.Icon to null and disposing:

notifyIcon.Icon = null;
notifyIcon.Dispose();
Application.DoEvents();

And consider Environment.Exit(0); instead of Process.GetCurrentProcess().Kill().

Upvotes: 2

Sinaesthetic
Sinaesthetic

Reputation: 12192

I don't think WPF has it's own NotifyIcon, does it? If you're using the 3rd party Harcodet.Wpf.TaskbarNotification, then try this:

In order to prevent my app from closing when the window is closed (run in background), I separated the logic for closing the window (hitting the x button in the upper right) and actually shutting it down (through the context menu). To make this work, make your context menu set _isExplicitClose to true. Otherwise, it'll just hide the window and continue to run.

What this does is, on explicit close, hide tray icon and the form before closing. This way the icon isn't hanging around after the application is shutdown.

private bool _isExplicitClose;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);

    if (!_isExplicitClose)
    {
        e.Cancel = true;
        Hide();
    }
}

protected void QuitService(object sender, RoutedEventArgs e)
{
   _isExplicitClose = true;
   TaskbarIcon.Visibility = Visibility.Hidden;
   Close();
}

Upvotes: 2

Lanello
Lanello

Reputation: 59

i can tell you can solve the problem simply using the .dispose() method, but that is not called if you kill the process instead of exit the application.

please refer to Application.Exit if you have built a simple Windows Form application else refer to Environment.Exit that is more general.

Upvotes: 1

vic
vic

Reputation: 450

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

Upvotes: 0

Jason Dias
Jason Dias

Reputation: 771

You can either set

notifyIcon1.Visible = false;

OR

notifyIcon.Icon = null;

in the form closing event.

Upvotes: 56

Matthew Watson
Matthew Watson

Reputation: 109567

This is normal behaviour, unfortunately; it's due to the way Windows works. You can'r really do anything about it.

See Issue with NotifyIcon not dissappearing on Winforms App for some suggestions, but none of them ever worked for me.

Also see Notify Icon stays in System Tray on Application Close

Microsoft have marked this as "won't fix" on Microsoft Connect.

Upvotes: 5

Related Questions