I have a system tray app which gets the status of a windows service,
and displays the appropriate icon for the status of the service in the
systray.
the problem I am having is the icon disapears under certain conditions,
but the exe is still listed in the processes leading me to believe the
app is still running.
It fires up perfectly on boot, however if the user logs off, and then
logs back in (not reboot), or if the system goes to standby, or
hibernate, the icon is lost (hidden) from the system tray.
the app is loaded from the HKCU portion of the registry.
Like I say, it refreshes fine based on the status of the service, but
it disapears based on the criteria above.
ideas?
I have gone overboard in my code to try and force it to never lose the
icon, but can not pinpoint my error.
<snip>
Imports System.Diagnost ics
Imports System.Text
Imports System.Xml
Imports System.IO
Public Module MNotify
Private WithEvents mobNotifyIcon As NotifyIcon
Private WithEvents mobContextMenu As ContextMenu
Private WithEvents mobTimer As Timers.Timer
Private WithEvents mobXDaysTimer As Timers.Timer
Private mobServiceContr oller As
System.ServiceP rocess.ServiceC ontroller
Public lastfilewritten As String
Public logDirectory As String
Public lastfile As String
Public intVariance As Integer
Public lastfiledatewri tten As String
Public Sub Main()
Try
'//SET UP CONECTION TO SERVICE.
mobServiceContr oller = New
System.ServiceP rocess.ServiceC ontroller("MySe rvice")
'//KEEP NOTIFY ICON HIDDEN UNTIL ICON AND MENU IS SET.
mobNotifyIcon = New NotifyIcon
mobNotifyIcon.V isible = False
mobContextMenu = New ContextMenu
CreateMenu()
mobNotifyIcon.C ontextMenu = mobContextMenu
SetUpTimer()
SetUpXDaysTimer ()
mobNotifyIcon.V isible = True
Application.Run ()
Catch obEx As Exception
MsgBox("error starting systray app:" &
obEx.Message.To String, MsgBoxStyle.Cri tical)
End
End Try
End Sub
Private Sub SetUpTimer()
Try
mobTimer = New Timers.Timer
With mobTimer
..AutoReset = True
..Interval = 5000
'.Interval = 60000
..Start()
End With
Catch obEx As Exception
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("systray : can not setup timer: Service not
functioning..." )
End Try
End Sub
Private Sub SetUpXDaysTimer ()
Try
mobXDaysTimer = New Timers.Timer
With mobXDaysTimer
..AutoReset = True
'show every 4 hours, or 6 times a day (1000 * 60 * 60 *
4)
..Interval = 14400000
'.Interval = 30000
..Start()
End With
Catch obEx As Exception
MsgBox("systray : can not set up SetupXDaysTimer ")
End Try
End Sub
Private Sub CreateMenu()
Try
mobContextMenu. MenuItems.Add(N ew MenuItem("Launc h Utility",
New EventHandler(Ad dressOf LaunchUtility)) )
Catch obEx As Exception
MsgBox("systray : can not add menu")
End Try
End Sub
Private Sub GetServiceStatu s()
Try
'//REFRESH PROPERTIES BEFORE READING.
mobServiceContr oller.Refresh()
'//REFLECT STATUS IN THE CONTEXT MENU.
Select Case mobServiceContr oller.Status()
Case ServiceProcess. ServiceControll erStatus.Paused
mobNotifyIcon.I con = New Icon("mbNotifyP aused.ico")
mobNotifyIcon.T ext = "Scheduler - Paused..."
Case ServiceProcess. ServiceControll erStatus.Runnin g
mobNotifyIcon.I con = New Icon("mbNotifyO n.ico")
mobNotifyIcon.T ext = "Scheduler - Running..."
Case ServiceProcess. ServiceControll erStatus.Stoppe d
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
mobNotifyIcon.T ext = "Scheduler - Stopped..."
Case _
ServiceProcess. ServiceControll erStatus.Contin uePending, _
ServiceProcess. ServiceControll erStatus.PauseP ending, _
ServiceProcess. ServiceControll erStatus.StartP ending, _
ServiceProcess. ServiceControll erStatus.StopPe nding
mobNotifyIcon.I con = New Icon("mbNotifyP aused.ico")
mobNotifyIcon.T ext = "Scheduler - Pending..."
Case Else
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("Service not functioning..." )
mobNotifyIcon.T ext = "Error..."
End Select
mobNotifyIcon.V isible = True
Catch obEx As Exception
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("Service not functioning..." )
mobNotifyIcon.T ext = "Error..."
End Try
End Sub
Public Sub mobTimer_Elapse d(ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles mobTimer.Elapse d
Try
GetServiceStatu s()
Catch obEx As Exception
'MsgBox("systra y: Can not get service status:" &
obEx.Message)
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("Service not functioning..." )
End Try
End Sub
Public Sub mobXDaysTimer_E lapsed(ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles mobXDaysTimer.E lapsed
Try
ShowPop()
Catch obEx As Exception
End Try
End Sub
Private Sub ExitController( ByVal sender As Object, ByVal e As
EventArgs)
Try
mobTimer.Stop()
mobTimer.Dispos e()
mobNotifyIcon.V isible = False
mobNotifyIcon.D ispose()
mobServiceContr oller.Dispose()
Application.Exi t()
Catch obEx As Exception
End Try
End Sub
</snip>
and displays the appropriate icon for the status of the service in the
systray.
the problem I am having is the icon disapears under certain conditions,
but the exe is still listed in the processes leading me to believe the
app is still running.
It fires up perfectly on boot, however if the user logs off, and then
logs back in (not reboot), or if the system goes to standby, or
hibernate, the icon is lost (hidden) from the system tray.
the app is loaded from the HKCU portion of the registry.
Like I say, it refreshes fine based on the status of the service, but
it disapears based on the criteria above.
ideas?
I have gone overboard in my code to try and force it to never lose the
icon, but can not pinpoint my error.
<snip>
Imports System.Diagnost ics
Imports System.Text
Imports System.Xml
Imports System.IO
Public Module MNotify
Private WithEvents mobNotifyIcon As NotifyIcon
Private WithEvents mobContextMenu As ContextMenu
Private WithEvents mobTimer As Timers.Timer
Private WithEvents mobXDaysTimer As Timers.Timer
Private mobServiceContr oller As
System.ServiceP rocess.ServiceC ontroller
Public lastfilewritten As String
Public logDirectory As String
Public lastfile As String
Public intVariance As Integer
Public lastfiledatewri tten As String
Public Sub Main()
Try
'//SET UP CONECTION TO SERVICE.
mobServiceContr oller = New
System.ServiceP rocess.ServiceC ontroller("MySe rvice")
'//KEEP NOTIFY ICON HIDDEN UNTIL ICON AND MENU IS SET.
mobNotifyIcon = New NotifyIcon
mobNotifyIcon.V isible = False
mobContextMenu = New ContextMenu
CreateMenu()
mobNotifyIcon.C ontextMenu = mobContextMenu
SetUpTimer()
SetUpXDaysTimer ()
mobNotifyIcon.V isible = True
Application.Run ()
Catch obEx As Exception
MsgBox("error starting systray app:" &
obEx.Message.To String, MsgBoxStyle.Cri tical)
End
End Try
End Sub
Private Sub SetUpTimer()
Try
mobTimer = New Timers.Timer
With mobTimer
..AutoReset = True
..Interval = 5000
'.Interval = 60000
..Start()
End With
Catch obEx As Exception
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("systray : can not setup timer: Service not
functioning..." )
End Try
End Sub
Private Sub SetUpXDaysTimer ()
Try
mobXDaysTimer = New Timers.Timer
With mobXDaysTimer
..AutoReset = True
'show every 4 hours, or 6 times a day (1000 * 60 * 60 *
4)
..Interval = 14400000
'.Interval = 30000
..Start()
End With
Catch obEx As Exception
MsgBox("systray : can not set up SetupXDaysTimer ")
End Try
End Sub
Private Sub CreateMenu()
Try
mobContextMenu. MenuItems.Add(N ew MenuItem("Launc h Utility",
New EventHandler(Ad dressOf LaunchUtility)) )
Catch obEx As Exception
MsgBox("systray : can not add menu")
End Try
End Sub
Private Sub GetServiceStatu s()
Try
'//REFRESH PROPERTIES BEFORE READING.
mobServiceContr oller.Refresh()
'//REFLECT STATUS IN THE CONTEXT MENU.
Select Case mobServiceContr oller.Status()
Case ServiceProcess. ServiceControll erStatus.Paused
mobNotifyIcon.I con = New Icon("mbNotifyP aused.ico")
mobNotifyIcon.T ext = "Scheduler - Paused..."
Case ServiceProcess. ServiceControll erStatus.Runnin g
mobNotifyIcon.I con = New Icon("mbNotifyO n.ico")
mobNotifyIcon.T ext = "Scheduler - Running..."
Case ServiceProcess. ServiceControll erStatus.Stoppe d
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
mobNotifyIcon.T ext = "Scheduler - Stopped..."
Case _
ServiceProcess. ServiceControll erStatus.Contin uePending, _
ServiceProcess. ServiceControll erStatus.PauseP ending, _
ServiceProcess. ServiceControll erStatus.StartP ending, _
ServiceProcess. ServiceControll erStatus.StopPe nding
mobNotifyIcon.I con = New Icon("mbNotifyP aused.ico")
mobNotifyIcon.T ext = "Scheduler - Pending..."
Case Else
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("Service not functioning..." )
mobNotifyIcon.T ext = "Error..."
End Select
mobNotifyIcon.V isible = True
Catch obEx As Exception
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("Service not functioning..." )
mobNotifyIcon.T ext = "Error..."
End Try
End Sub
Public Sub mobTimer_Elapse d(ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles mobTimer.Elapse d
Try
GetServiceStatu s()
Catch obEx As Exception
'MsgBox("systra y: Can not get service status:" &
obEx.Message)
mobNotifyIcon.I con = New Icon("mbNotifyO ff.ico")
MsgBox("Service not functioning..." )
End Try
End Sub
Public Sub mobXDaysTimer_E lapsed(ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles mobXDaysTimer.E lapsed
Try
ShowPop()
Catch obEx As Exception
End Try
End Sub
Private Sub ExitController( ByVal sender As Object, ByVal e As
EventArgs)
Try
mobTimer.Stop()
mobTimer.Dispos e()
mobNotifyIcon.V isible = False
mobNotifyIcon.D ispose()
mobServiceContr oller.Dispose()
Application.Exi t()
Catch obEx As Exception
End Try
End Sub
</snip>