Modifying the NotifyIcon.Icon Property from Within A Service Application

G

Guest

I am in the process of developing a Windows Service which will: (1) monitor multiple network shares; (2) marshal text file transfers into an SQL Server 2000 instance; and (3) provide messaging services via email and a customized event log viewer. An additional goal is to have the service provide a visual status indicator via an icon to be located in the Taskbar status area. The NotifyIcon component appears to be a logical candidate and worked perfectly from within a sample Windows Forms application. I was able to programmatically manipulate the NotifyIcon.Icon property, and thus change the icon in the Taskbar status area, without difficulty

However, my efforts at providing simliar functionality from within a sample Windows Service application have resulted in the following exception message

"Exception has been thrown by the target of an invocation.

The code from the Windows Service OnStart procedure is as follows

Protected Overrides Sub OnStart(ByVal args() As String
Tr
Dim rm As ResourceManager = ResourceManager.CreateFileBasedResourceManager("TrunkDataIcons", "c:\TrunkData", Nothing
Me.NotifyIcon1.Icon = CType(rm.GetObject("TrunkDataStarting"), Drawing.Icon
Me.NotifyIcon1.Visible = Tru
Catch ex As Exceptio
'service application; write the exception message to a text fil
Dim iFile As Integer = FreeFile(
FileOpen(iFile, "c:\exception.txt", OpenMode.Output
WriteLine(iFile, ex.Message.ToString
FileClose(iFile
End Tr
End Su

Since this is a Windows Service application, the 'c:\exception.txt' text file provides a handy location for exception messages.

The 'TrunkDataIcons.resources' file contains seven System.Drawing.Icon object references directed to seven distinct '.ico' files. For example, the 'TrunkDataStarting' object refers to the 'TrunkData_Starting.ico' file within the resources file. I validated the contents of the 'TrunkDataIcons' resource file through the use of a sample Windows Forms application. A command button was assigned to each of the seven System.Drawing.Icon objects contained within the resource file and a Click event was created for each to allow the NotifyIcon.Icon property, and hence the icon in the tray, to be changed accordingly. As was mentioned earlier, this worked perfectly and served to validate the use of the ResourceManager object and the associated resource file.

However, attempting to utilize the same code construct and assign the contents of the 'TrunkDataIcons' resource file to the NotifyIcon.Icon property via the ResourceManager within the Windows Service application results in the aforementioned exception. I am at my wits end. Any assistance would be greatly appreciated

Best Regards
Randall Powel
 
J

Jay B. Harlow [MVP - Outlook]

Randall,
Since this is a Windows Service application, the 'c:\exception.txt' text
file provides a handy location for exception messages.
For a Windows Service I would recommend the Windows Event Log for exception
messages, over a text file.
Catch ex As Exception
EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error)
End Try

Notice that the Service gives you an EventLog property that is configured to
go to Application - My Service, you can override the property to return your
own EventLog object (so you can change the Log and/or Source).

Note using Ex.ToString(), as opposed to Ex.Message will include the stack
trace in the message written, I suspect the stack trace will point to the
NotifyIcon1 object. As services by default are not allowed to interact with
the desktop. Nor should Services interact with desktops!

What I would suggest is your service write the what state its in to a
Performance counter (I would use an Enum, that represented which Icon to
show).

Then I would write a Manager program (Windows Application) that read this
Performance Counter and displayed the appropriate icon. This Manager program
could also retrieve and display any event log messages the service wrote.

I would also consider adding additional performance counters to monitor the
amount of work the service is doing...

Hope this helps
Jay

Randall Powell said:
I am in the process of developing a Windows Service which will: (1)
monitor multiple network shares; (2) marshal text file transfers into an SQL
Server 2000 instance; and (3) provide messaging services via email and a
customized event log viewer. An additional goal is to have the service
provide a visual status indicator via an icon to be located in the Taskbar
status area. The NotifyIcon component appears to be a logical candidate and
worked perfectly from within a sample Windows Forms application. I was able
to programmatically manipulate the NotifyIcon.Icon property, and thus change
the icon in the Taskbar status area, without difficulty.
However, my efforts at providing simliar functionality from within a
sample Windows Service application have resulted in the following exception
message:
"Exception has been thrown by the target of an invocation."

The code from the Windows Service OnStart procedure is as follows:

Protected Overrides Sub OnStart(ByVal args() As String)
Try
Dim rm As ResourceManager =
ResourceManager.CreateFileBasedResourceManager("TrunkDataIcons",
"c:\TrunkData", Nothing)
Me.NotifyIcon1.Icon = CType(rm.GetObject("TrunkDataStarting"), Drawing.Icon)
Me.NotifyIcon1.Visible = True
Catch ex As Exception
'service application; write the exception message to a text file
Dim iFile As Integer = FreeFile()
FileOpen(iFile, "c:\exception.txt", OpenMode.Output)
WriteLine(iFile, ex.Message.ToString)
FileClose(iFile)
End Try
End Sub

Since this is a Windows Service application, the 'c:\exception.txt' text
file provides a handy location for exception messages.
The 'TrunkDataIcons.resources' file contains seven System.Drawing.Icon
object references directed to seven distinct '.ico' files. For example, the
'TrunkDataStarting' object refers to the 'TrunkData_Starting.ico' file
within the resources file. I validated the contents of the 'TrunkDataIcons'
resource file through the use of a sample Windows Forms application. A
command button was assigned to each of the seven System.Drawing.Icon objects
contained within the resource file and a Click event was created for each to
allow the NotifyIcon.Icon property, and hence the icon in the tray, to be
changed accordingly. As was mentioned earlier, this worked perfectly and
served to validate the use of the ResourceManager object and the associated
resource file.
However, attempting to utilize the same code construct and assign the
contents of the 'TrunkDataIcons' resource file to the NotifyIcon.Icon
property via the ResourceManager within the Windows Service application
results in the aforementioned exception. I am at my wits end. Any
assistance would be greatly appreciated.
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?UmFuZGFsbCBQb3dlbGw=?= said:
I am in the process of developing a Windows Service which will: (1)
monitor multiple network shares; (2) marshal text file transfers into an
SQL Server 2000 instance; and (3) provide messaging services via email
and a customized event log viewer. An additional goal is to have the
service provide a visual status indicator via an icon to be located in
the Taskbar status area. The NotifyIcon component appears to be a
logical candidate and worked perfectly from within a sample Windows
Forms application. I was able to programmatically manipulate the
NotifyIcon.Icon property, and thus change the icon in the Taskbar status
area, without difficulty.

Instead of adding the icon through the service, create a 2nd Windows
Forms app that communicates with the service (remoting, sockets or
something similar) and displays the icon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top