none
PowerShell cmdlet NotifyIcon still visible after cmdlet exit.

    Question

  • I am writing a PowerShell cmdlet which puts an icon in the Navigation bar, but it stays there until the PowerShell session ends. The timeout disappears after the 10000ms but the icon still remains until the PS session exits, so what is needed please? This is the code:

    Dim objNotifyIcon As New System.Windows.Forms.NotifyIcon
    Protected Overrides Sub ProcessRecord()
       ...
       objNotifyIcon.Icon = My.Resources.exclamation
       objNotifyIcon.Text = "Testing"
       objNotifyIcon.Visible = True
       objNotifyIcon.BalloonTipIcon = Windows.Forms.ToolTipIcon.Info
       objNotifyIcon.BalloonTipTitle = "Information"
       objNotifyIcon.BalloonTipText  = "Application Data"
       objNotifyIcon.ShowBalloonTip(10000)
    End Sub

    Protected Overrides Sub EndProcessing()
        If Not objNotifyIcon Is Nothing Then
           objNotifyIcon.Visible = False                                         
           objNotifyIcon.Icon = Nothing
           objNotifyIcon.Dispose()
        End If
    End Sub  
    Tuesday, January 07, 2014 5:51 AM

All replies

  • Hi,

    Welcome to MSDN.

    You could add a line as following code to test:

    Protected Overrides Sub EndProcessing()
         If Not objNotifyIcon Is Nothing Then
            objNotifyIcon.Visible = False                                          
            objNotifyIcon.Icon = Nothing
            objNotifyIcon.Dispose()
            objNotifyIcon = Nothing
         End If
     End Sub  
    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 08, 2014 3:36 AM
  • I just tried that but the icon still remains.
    Wednesday, January 08, 2014 6:09 AM
  • Hi,

    Before we making sure whether this issue is related to GC, I suggest you test it with the following code:

    Protected Overrides Sub EndProcessing()
           If Not objNotifyIcon Is Nothing Then
                objNotifyIcon.Visible = False
                objNotifyIcon.Icon = Nothing
                objNotifyIcon.BalloonTipIcon = ToolTipIcon.None
                objNotifyIcon.Dispose()
                objNotifyIcon = Nothing
                MsgBox("done")
            End If
     End Sub  

    Will that messagebox show? If not, you could use FormClosing event:

     Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            If Not objNotifyIcon Is Nothing Then
                objNotifyIcon.Visible = False
                objNotifyIcon.Icon = Nothing
                objNotifyIcon.Dispose()
                objNotifyIcon = Nothing
            End If
        End Sub


    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, January 08, 2014 6:26 AM
  • Hi,

    Before we making sure whether this issue is related to GC, I suggest you test it with the following code:

    Protected Overrides Sub EndProcessing()
           If Not objNotifyIcon Is Nothing Then
                objNotifyIcon.Visible = False
                objNotifyIcon.Icon = Nothing
                objNotifyIcon.BalloonTipIcon = ToolTipIcon.None
                objNotifyIcon.Dispose()
                objNotifyIcon = Nothing
                MsgBox("done")
            End If
     End Sub  

    Will that messagebox show? If not, you could use FormClosing event:

     Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            If Not objNotifyIcon Is Nothing Then
                objNotifyIcon.Visible = False
                objNotifyIcon.Icon = Nothing
                objNotifyIcon.Dispose()
                objNotifyIcon = Nothing
            End If
        End Sub


    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Hi,

    I edited that reply, you could test that suggestion.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 08, 2014 7:32 AM
  •  

    Tried adding this line (objNotifyIcon.BalloonTipIcon = Windows.Forms.TooltipIcon.None), but the Icon still remains and only disappears after the PS session has ended and the mouse is hovered over it.

    There is no Form1 here so no closing Event to use; but a WriteWarning("DONE") produces DONE before the Cmdlet exits.

    • Edited by Trader_Horn Wednesday, January 08, 2014 2:08 PM
    Wednesday, January 08, 2014 2:08 PM
  •  

    Tried adding this line (objNotifyIcon.BalloonTipIcon = Windows.Forms.TooltipIcon.None), but the Icon still remains and only disappears after the PS session has ended and the mouse is hovered over it.

    There is no Form1 here so no closing Event to use; but a WriteWarning("DONE") produces DONE before the Cmdlet exits.

    Do you have a contextmenustrip created as an icon in the taskbar? I have had the application successfully disappear in a certain amount of time when doing this.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.co.nr

    Wednesday, January 08, 2014 6:11 PM
  • The ContextMenu always appears in the Taskbar and times out after 10000ms, but the icon will not disappear until the PS session exits. I have PowerShell running minimized permanently; only closing the session at shutdown.
    Wednesday, January 08, 2014 11:43 PM
  • Hi,

    I am afraid that since the messagebox could show when cmdlet exited, then these lines has been done, but how to deal with them will based on GC because these lines are managed. So I don't think there is a workaround for this issue.

    The following lines are not suggested, but you could have a try:

    Protected Overrides Sub EndProcessing()
           If Not objNotifyIcon Is Nothing Then
                objNotifyIcon.Visible = False
                objNotifyIcon.Icon = Nothing
                objNotifyIcon.BalloonTipIcon = ToolTipIcon.None
                objNotifyIcon.Dispose()
                objNotifyIcon = Nothing
                MsgBox("done")
                GC.Collect()
            End If
     End Sub  

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 09, 2014 9:13 AM
  • I tried that last line but still no use. Icon is firmly stuck until PS session exits. Thanks anyway.
    Thursday, January 09, 2014 1:02 PM
  • So strange that I tested it just with

    objNotifyIcon.Dispose()
    objNotifyIcon
    = Nothing

    and it disappeared as expected.

    If you test it in the other PC, will it still couldn't disappear?


    remember make the reply as answer and vote the reply as helpful if it helps.

    Friday, January 10, 2014 6:32 AM
  • Just confirm that you are running this from a compiled PowerShell cmdlet and not a Windows Form?
    Friday, January 10, 2014 1:42 PM
  • Windows Form

    remember make the reply as answer and vote the reply as helpful if it helps.

    Monday, January 13, 2014 5:56 AM