0

I have written a small application which mainly runs in the system tray. The app shows a notifyicon at startup, which works fine. If I start an action from the ContextMenuStrip, the notifyicon should change its icon and change back to original when a process is finished.

I am experiencing a weird behaviour: The icon duplicates itself on the first action and changes its icon properly. Now I am running two notifyicons in the system tray: One of them working properly and changing its icon when the tasks finish, the other one simply stays there and functions - but without an icon change.

I am not sure where the second icon comes from.

This is part of the code I am creating the notifyicon at startup with:

Public Class NotifyIconManager
    Private notifyIcon As NotifyIcon
    Public Sub New()
        notifyIcon = New NotifyIcon
        notifyIcon.Icon = My.Resources.icon_normal
        notifyIcon.Text = "Ready."
        notifyIcon.ContextMenuStrip = Main.notifyicon_ContextMenuStrip
        notifyIcon.Visible = True
        AddHandler notifyIcon.MouseDoubleClick, AddressOf DoubleClick
    End Sub

    Public Sub SetNotifyIconUsage()
        notifyIcon.Icon = My.Resources.icon_busy
        notifyIcon.Text = "Working."
    End Sub

    Public Sub SetNotifyIconIdle()
        notifyIcon.Icon = My.Resources.icon_normal
        notifyIcon.Text = "Ready."
    End Sub
End Class

I am changing the icon with calling boot.NotifyIconManager.SetNotifyIconUsage() or boot.NotifyIconManager.SetNotifyIconIdle() before starting the backgroundworker. One instance of the class is created in my main startup form:

Public NotifyIconManager As New NotifyIconManager

Now the question is: How can I get rid of the duplicated notifyicon?

I have already tried disposing the notifyicon and creating a new one everytime I want to change the icon, but this creates a duplicate as well. Setting the icon to Nothing and then back to a new icon also does not work for me.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
max07
  • 141
  • 2
  • 16
  • Do any of the icons disappear from the tray when you mouse over them? If not then I think that it's safe to say that you are creating multiple `NotifyIcon` objects, even if you think you aren't. You should be able to test that with a breakpoint. – jmcilhinney Apr 13 '20 at 16:49
  • Also, don't keep accessing properties of `My.Resources` over and over. You are creating new `Icon` objects each time. Access each property once, assign the result to a variable and then use that each time. Make sure to dispose those objects when you're done with them too. – jmcilhinney Apr 13 '20 at 16:51
  • Please don't put tags in your title. That's what tags are for. – jmcilhinney Apr 13 '20 at 16:52
  • The icons do not disappear when hovering over them. Both icons work properly, but only one of them changes. It will always stay with two icons in total. The NotifyIconManager gets created only one time at the very first step inside the code. The icon only gets changed with the two methods described above. The breakpoint didn't help much, is there any way to see the instances of classes? – max07 Apr 13 '20 at 17:08
  • You can GetHashcode on a class which, if not overridden, returns its memory address. Different hashcodes are different instances – Caius Jard Apr 13 '20 at 20:44
  • Have you added a `NotifyIcon` to a form in the designer anywhere? – jmcilhinney Apr 14 '20 at 00:11

1 Answers1

0

After some debugging (thanks for making me use breakpoints jmcilhinney!), I discovered that I actually was creating a second main form by accident. This resulted into two notifyicons.

max07
  • 141
  • 2
  • 16