469,599 Members | 2,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

Difficulties getting notification Icon to work with a Service

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(ServicesToRu n)

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(TimerServ ice))
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","Re petition
#" & _
Counter.ToString
(),EventLogEntryType.Information,5000,1)
Diagnostics.EventLog.WriteEntry("TimerService","Is visible?
" & _

Me.thisServiceicon.Visible,EventLogEntryType.Infor mation,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

Jan 23 '07 #1
7 3824

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! :(
Brian wrote:
hello Gentlemen,
Jan 23 '07 #2
"Brian" <ha******@ix.netcom.comschrieb
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

Jan 23 '07 #3
Armin Zingler wrote:
"Brian" <ha******@ix.netcom.comschrieb
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.
And note that this will no longer work under Windows Vista. Making a
service interact with the desktop is discouraged. The recommended
approach is to create a second program that communicates with the
service and displays the icon.

Chris

Jan 23 '07 #4

Armin Zingler wrote:
". See 2nd folder in the service property window in
service manager."
Armin
Not sure what that means...
Yes gentlemen i am aware this is not a sugested means for a solution,
just thought since Microsoft Press printed this possibility in their
Cookbook then it should at least work once.....

any other suggestion?

~Brian

Jan 23 '07 #5
"Brian" <ha******@ix.netcom.comschrieb
>
Armin Zingler wrote:
". See 2nd folder in the service property window in
service manager."
Armin

Not sure what that means...
Yes gentlemen i am aware this is not a sugested means for a
solution, just thought since Microsoft Press printed this
possibility in their Cookbook then it should at least work once.....

any other suggestion?

Does this mean you have already enabled this option and it still doesn't
work? If you have, I have no other suggestion.
Armin

Jan 23 '07 #6

Armin Zingler wrote:
Does this mean you have already enabled this option and it still doesn't
work? If you have, I have no other suggestion.

Armin
I do not see a property for 'Interact with Desktop' in my Visual Studio
2005 interface, can oyu give me a little more advice on where this
feature is at?

thanks

~Brian

Jan 23 '07 #7
"Brian" <ha******@ix.netcom.comschrieb
>
Armin Zingler wrote:
Does this mean you have already enabled this option and it still
doesn't work? If you have, I have no other suggestion.

Armin

I do not see a property for 'Interact with Desktop' in my Visual
Studio 2005 interface, can oyu give me a little more advice on where
this feature is at?

quote: "...in service manager"

Start menu - run: services.msc
open service properties, 2nd tab page, check "interact with desktop" (or
similar text)
Armin

Jan 23 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by kapcreations | last post: by
reply views Thread by Eric | last post: by
1 post views Thread by =?Utf-8?B?UmFmaQ==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.