Difficulties getting notification Icon to work with a Service

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

hello Gentlemen,

been trying to get a shell program working where I have a Windows
service that also has a Notification icon in the system tray.....

I got this idea from microsofts Visual Basic .Net cookbook, but cannot
seem to get it to work.
Also I am using the Visual Studio 2005 not 2003 as original book was I
believe created for.
and I added an icon resource into the project also. You can add your
own of course, just call it app.ico or change the code acordingly I
would assume.

here is the code.... I added line continuation Underscores for
readability within the google group, not for actual code
processing....... any comments will help.


Imports System.ServiceProcess
Imports System.Timers
Imports Microsoft.Win32
Imports System.Drawing
Imports System.Windows.Forms


Public Class TimerService
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
TimerService()}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer
Private thisContextMenu As System.Windows.Forms.ContextMenu
Friend WithEvents thisMenuItem As System.Windows.Forms.MenuItem
Friend WithEvents thisServiceicon As
System.Windows.Forms.NotifyIcon

' NOTE: The following procedure is required by the Component
Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

me.components = New System.ComponentModel.Container()
Me.thisContextMenu = New System.Windows.Forms.ContextMenu
Me.thisMenuItem = New System.Windows.Forms.MenuItem

' Initialize contextMenu1
Me.thisMenuItem.Text = "E&xit"
Me.thisContextMenu.MenuItems.Add(0,Me.thisMenuItem)

Dim resources As System.Resources.ResourceManager = New _

System.Resources.ResourceManager(GetType(TimerService))
Me.thisServiceicon = New
System.Windows.Forms.NotifyIcon(Me.components)

' thisServiceicon
Me.thisServiceicon.Icon =
CType(resources.GetObject("app.Icon"), System.Drawing.Icon)
Me.thisServiceicon.ContextMenu = Me.thisContextMenu

Me.thisServiceicon.Text = "Initialize"
Me.thisServiceicon.BalloonTipText = "Timer Service"
Me.thisServiceicon.BalloonTipTitle = "Timer Service"
Me.thisServiceicon.Visible = True

Me.ServiceName = "TimerService"

End Sub

#End Region

' This fires every 10 seconds.
Private WithEvents ServiceTimer As New Timers.Timer(10000)
Private Counter As Integer

Protected Overrides Sub OnStart(ByVal args() As String)

ServiceTimer.Start()
me.AutoLog = True

Me.thisServiceicon.ShowBalloonTip(1, "Started " +
TimerService.APP_TITLE , _
"You can double click on this icon
to configure settings.\nClick _
here if you do not wish to see this
message again.", CType(1, _
Windows.Forms.ToolTipIcon))

End Sub

Protected Overrides Sub OnStop()
ServiceTimer.Stop()

End Sub

Private Sub DoWork(ByVal sender As Object, ByVal e As
ElapsedEventArgs) Handles ServiceTimer.Elapsed

Dim myFile As New System.IO.FileStream("c:\ServiceTest.txt",
IO.FileMode.Append )
Dim myStream As New system.IO.StreamWriter(myFile)

Counter += 1
'Debug.WriteLine("Repetition #" & Counter.ToString())

Try
myStream.Write("Test #" & Counter.ToString() & vbCRLF)

Me.EventLog.Source = "TimerService"
Diagnostics.EventLog.WriteEntry("TimerService","Repetition
#" & _
Counter.ToString
(),EventLogEntryType.Information,5000,1)
Diagnostics.EventLog.WriteEntry("TimerService","Is visible?
" & _

Me.thisServiceicon.Visible,EventLogEntryType.Information,5000,1)

Catch Ex As Exception
Debug.WriteLine(Ex.ToString())

Finally
myStream.Flush()
myFile.Close()

End Try

End Sub

Private Sub thisServiceicon_DoubleClick(Sender as object, e as
EventArgs) _
handles thisServiceicon.DoubleClick
' Show the selected item when the user double clicks on the
notify icon.


end sub

Private Sub thisMenuItem_Click(Sender as object, e as EventArgs)
handles thisMenuItem.Click
' Close the service.
me.OnStop

end sub

End Class
 
Brian said:
hello Gentlemen,

been trying to get a shell program working where I have a Windows
service that also has a Notification icon in the system tray.....

I got this idea from microsofts Visual Basic .Net cookbook, but
cannot seem to get it to work.

Doesn't work means what? Compilation error? Runtime excpetion? Wrong
behavior?

In general: Service <> UI. If you want to do it, you must allow the service
to "interact with desktop". See 2nd folder in the service property window in
service manager.


Armin
 
Forgot to tell you what the problem is!!! :)

The icon will not appear for me.....
I register it with the INSTALLUTIL executable for .Net 2.0
the event log gets written too and the text file too.

but no icon! :(
 
Back
Top