472,326 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,326 software developers and data experts.

Modifying the NotifyIcon.Icon Property from Within A Service Application

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("Tr unkDataIcons", "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
Nov 20 '05 #1
2 2554
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" <an*******@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.com... 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("Tr unkDataIcons",
"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.
Best Regards,
Randall Powell

Nov 20 '05 #2
* "=?Utf-8?B?UmFuZGFsbCBQb3dlbGw=?=" <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Phil Galey | last post by:
VB.NET 2002 on Windows 2000 SP 3 When I start my program, the NotifyIcon appears in the tray, as it should. However, when I close the program,...
3
by: Me | last post by:
I'm getting a NullReferenceException in Unknown Module when I follow the below steps to create a simple NotifyIcon app that creates the context...
0
by: Dick Abram | last post by:
I have a project that uses the NotifyIcon to put an icon in the system tray. At some point in the application I change the .Icon property of the...
3
by: Seymen Ertas | last post by:
Hey anyone seen this before ? I followed examples from microsoft and added a notifyicon to my windows service project and i can't get the icon to...
2
by: Derrick | last post by:
I've been working on an application which has a NotifyIcon (system tray icon), and a corresponding ContextMenu. I want to be able to update this...
0
by: Neo | last post by:
I have created a Windows Service that shows a notifyicon in the system tray. I have also created a context menu for the notifyicon and implemented...
1
soulstace
by: soulstace | last post by:
Hi all, I have a project with two forms. Call them form1 and form2. On form1, I have a notifyIcon which appears in the system tray when the...
1
by: \Ji Zhou [MSFT]\ | last post by:
Hello Jason, Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou and I will be working on this issue with you. I have...
8
by: starrysky | last post by:
I have a program which puts an icon in the notification area and has a menu associated with it available by right clicking on the icon. I want the...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.