dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.38k stars 971 forks source link

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

Open paul1956 opened 2 years ago

paul1956 commented 2 years ago

.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
merriemcgaw commented 2 years ago

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?

paul1956 commented 2 years ago

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

Symbai commented 2 years ago

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.

paul1956 commented 2 years ago

@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?

elachlan commented 11 months ago

@paul1956 Does disposing of the NotifyIcon on FormClose work?

paul1956 commented 11 months ago

@elachlan yes, below works in .Net 7

Me.NotifyIcon1?.Dispose()

elachlan commented 11 months ago

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

paul1956 commented 11 months ago

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.