473,696 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 programmaticall y 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 .CreateFileBase dResourceManage r("TrunkDataIco ns", "c:\TrunkDa ta", Nothing
Me.NotifyIcon1. Icon = CType(rm.GetObj ect("TrunkDataS tarting"), 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.t xt", OpenMode.Output
WriteLine(iFile , ex.Message.ToSt ring
FileClose(iFile
End Tr
End Su

Since this is a Windows Service application, the 'c:\exception.t xt' 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 'TrunkDataStart ing' object refers to the 'TrunkData_Star ting.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 2645
Randall,
Since this is a Windows Service application, the 'c:\exception.t xt' 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.WriteE ntry(ex.ToStrin g(), EventLogEntryTy pe.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*******@disc ussions.microso ft.com> wrote in message
news:8C******** *************** ***********@mic rosoft.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 programmaticall y 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 .CreateFileBase dResourceManage r("TrunkDataIco ns",
"c:\TrunkDa ta", Nothing) Me.NotifyIcon1. Icon = CType(rm.GetObj ect("TrunkDataS tarting"), 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.t xt", OpenMode.Output )
WriteLine(iFile , ex.Message.ToSt ring)
FileClose(iFile )
End Try
End Sub

Since this is a Windows Service application, the 'c:\exception.t xt' 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
'TrunkDataStart ing' object refers to the 'TrunkData_Star ting.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?UmFuZGFsbCB Qb3dlbGw=?=" <an*******@disc ussions.microso ft.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 programmaticall y 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
7361
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, the NotifyIcon remains until I hover over it with my mouse. If I don't hover over it to make it disappear and then open the program again, another one is added, resulting in a multiplicity of the icon in the tray. I've even tried setting its Visible property to false, followed by a call to the...
3
2107
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 menu on the fly(see a little analysis after the steps). 1. Create a new Windows Forms C# solution (I called mine DBChanger). 2. Replace Form1.cs code with this: -------------------- using System;
0
1311
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 object to another Icon object but instead of changing the tray icon to the new Icon a new icon appears in the system tray next to the original icon and the new icon will not resond to any events. Has anyone seen this?? Code snippet.... private Icon mDirIcon = new...
3
8408
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 show up. Here is a checklist of what i have tried so far: 1) Tried to make the icon 16x16 2) Made sure the icon was embedded into the exe. 3) Made sure the visible flag was set to true. 4) Included the content output from the project in the installer.
2
2925
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 menu dynamically. However, when I make changes to the menu, it seems to disappear. This only breaks when the context menu is tied to a NotifyIcon - not to any other control. Below is a C# file for a form which should illustrate this (at least it does on my machine!). The same contextmenu is...
0
2018
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 the notifyicons click event. The notifyicon does appears correctly in the system tray while the service is running but the context menu does not pop up when the icon is right clicked. Nor does the click event fire when the icon is clicked. Any suggestions?
1
5351
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 application starts. On form2, I have a textbox, browse button, and an openFileDialog. Now,
1
4295
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 tried to but cannot reproduce your issue on my side. From your codes, I think the logical is, every time the Timer's elapsed event fires, we call the Check_For_Version_Update() function. From the Check_For_Version_Update() function, we judge a condition statement, and determine whether to call...
8
8491
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 menu items to be selected by single left clicks but I also want the user to be able to single left click on the icon itself to do some other functionality. Here's the problem - the single click on the menu first fires the notifyicon1_click() routine and then the menuitem1_click() routine. How do I...
0
8592
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9141
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8871
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8846
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7694
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1989
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.