winforms icon indicating copy to clipboard operation
winforms copied to clipboard

NotifyIcon is not deleted (stays in tray) when application closes

Open paul1956 opened this issue 2 years ago • 8 comments

.NET version

Microsoft.WindowsDesktop.App 6.0.3 and all previous

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Not that I know of

Issue description

If you create an iNotifyIcon and show it in tray, when application exits the icon stays in tray. Each time you restart app you get another icon and now have 2,3,4... They do disappear when you hover over them after the app exits. This also impacts Visual Studio where icon stays in tray if debugging session is stopped.

Steps to reproduce

In Form.Designer.vb

    Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
    '
    'NotifyIcon1
    '
    Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"),System.Drawing.Icon)
    Me.NotifyIcon1.Text = "Some Text"
    Me.NotifyIcon1.Visible = true
    Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)

Ther is a workaround but it should not be necessary, and all steps many not be required.

In Form.VB

Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    Me.NotifyIcon1.Visible = False
    Me.NotifyIcon1.Icon.Dispose()
    Me.NotifyIcon1.Icon = Nothing
    Me.NotifyIcon1.Visible = False
    Me.NotifyIcon1.Dispose()
    Application.DoEvents()
End Sub

paul1956 avatar Apr 09 '22 22:04 paul1956

Thanks for this issue @paul1956. Can you tell me which version(s) of Windows you are testing on? And to clarify, you don't believe this worked in .NET Framework 4.x either?

merriemcgaw avatar Apr 14 '22 16:04 merriemcgaw

Windows 10 Pro and Windows 11 Pro. I just starting using NotifyIcon last week but a quick search shows people with this issue over 10 years ago with lots of different workarounds, many don't work. The solution above while longer than necessary seems to always work except in Visual Studio as shown below where the app is not even running. This happens with C# and VB and the workaround works for both at runtime. image

paul1956 avatar Apr 14 '22 18:04 paul1956

If you hard kill a process (in VS you stop debugging which kills the process rather than closing it gracefully) and it has a tray icon the tray icon remains visible until you simply mouse hover. This is because the code for removing this icon is not executed because you killed the process. This has been like that for ages. In a specific Windows 11 build mouse hovering doesn't remove the icon, this was a bug and already fixed with a recent build.

This is by the way not Winforms related, WPF has the problem too.

Symbai avatar Apr 15 '22 16:04 Symbai

@Symbai that explains the killing of the app and stopping in Visual Studio, it does not explain the application just ending normally. is there any code in WinForms to handle a graceful exit in the normal case?

paul1956 avatar Apr 16 '22 02:04 paul1956

@paul1956 Does disposing of the NotifyIcon on FormClose work?

elachlan avatar Nov 03 '23 22:11 elachlan

@elachlan yes, below works in .Net 7

Me.NotifyIcon1?.Dispose()

paul1956 avatar Nov 05 '23 22:11 paul1956

And it resolves the issue? Also does the form include it in the designer generated dispose?

elachlan avatar Nov 05 '23 23:11 elachlan

And it resolves the issue?

With that line added everything works as expected in .Net 7.0. but even this should be unnecessary or documented as required as it was not required in Framework.

Also does the form include it in the designer generated dispose? The program exits so I think the answer is yes but that always worked. The program would always exit but the icon stayed until if was hovered over in the tray.

paul1956 avatar Nov 05 '23 23:11 paul1956