473,756 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

notify icon and context menu for Windows Service Application

Hi Friends,

I have developed a Windows service. Now i need icon for this service in
systray and context menu fo this icon. Can i do this?

With regards,
Lalit
Jul 21 '05 #1
7 10323
On Fri, 1 Apr 2005 03:51:03 -0800, Lalit wrote:
I have developed a Windows service. Now i need icon for this service in
systray and context menu fo this icon. Can i do this?


Yes. Create a normal Window Form application without a Form but with a
notify icon and a context menu associated with it. Have it communicate with
your windows service via .NET Remoting, raw sockets or some other way,
whichever is the most suitable for you. At install time, place this
application in the startup folder so that it starts automatically when a
user logs in and displays the notify icon.
Jul 21 '05 #2
Try something like this. Just make sure to add a resource file that
contains your icon file in it:

using System;
using System.Windows. Forms;

namespace TrayIcon
{
class TrayIcon
{

private System.Windows. Forms.ContextMe nu trayMenu;
private System.Windows. Forms.MenuItem mnuAbout;
private System.Windows. Forms.MenuItem mnuExit;
private System.Windows. Forms.NotifyIco n trayIcon;

public TrayIcon()
{
InitializeCompo nent();

Application.Run ();
}

private void InitializeCompo nent()
{
System.Resource s.ResourceManag er resources = new
System.Resource s.ResourceManag er(typeof(TrayI con));
this.mnuAbout = new System.Windows. Forms.MenuItem( );
this.mnuExit = new System.Windows. Forms.MenuItem( );
this.trayMenu = new System.Windows. Forms.ContextMe nu();
this.trayIcon = new System.Windows. Forms.NotifyIco n();

//
// mnuAbout
//
this.mnuAbout.I ndex = 8;
this.mnuAbout.T ext = "About";
this.mnuAbout.C lick += new System.EventHan dler(this.MenuC licks);
//
// mnuExit
//
this.mnuExit.In dex = 10;
this.mnuExit.Te xt = "Exit";
this.mnuExit.Cl ick += new System.EventHan dler(this.MenuC licks);

//
// trayMenu
//
this.trayMenu.M enuItems.AddRan ge(new System.Windows. Forms.MenuItem[]
{
this.mnuAbout,
this.mnuExit});
//
// trayIcon
//
this.trayIcon.C ontextMenu = this.trayMenu;
this.trayIcon.I con =
(System.Drawing .Icon)(resource s.GetObject("tr ayIcon.Icon"));
this.trayIcon.T ext = "TrayIcon Test";
this.trayIcon.V isible = true;
}

private void MenuClicks(obje ct sender, EventArgs e)
{
if (sender == mnuExit) Application.Exi t();
else if (sender == mnuAbout) MessageBox.Show ("About TrayIcon test
program");

}

public static void Main(string[] args)
{
new TrayIcon();
}
}
}

Jul 21 '05 #3
Hi,

I have tried this.
I am able to show the Systray icon by setting the allow service to interact
with Desktop property checked.

But the Context menu is not shown for that and i also tried the double click
event of notify icon. If i open the Exe of service directly then context menu
is shown.

Regards,
Lalit

"cmelnick" wrote:
Try something like this. Just make sure to add a resource file that
contains your icon file in it:

using System;
using System.Windows. Forms;

namespace TrayIcon
{
class TrayIcon
{

private System.Windows. Forms.ContextMe nu trayMenu;
private System.Windows. Forms.MenuItem mnuAbout;
private System.Windows. Forms.MenuItem mnuExit;
private System.Windows. Forms.NotifyIco n trayIcon;

public TrayIcon()
{
InitializeCompo nent();

Application.Run ();
}

private void InitializeCompo nent()
{
System.Resource s.ResourceManag er resources = new
System.Resource s.ResourceManag er(typeof(TrayI con));
this.mnuAbout = new System.Windows. Forms.MenuItem( );
this.mnuExit = new System.Windows. Forms.MenuItem( );
this.trayMenu = new System.Windows. Forms.ContextMe nu();
this.trayIcon = new System.Windows. Forms.NotifyIco n();

//
// mnuAbout
//
this.mnuAbout.I ndex = 8;
this.mnuAbout.T ext = "About";
this.mnuAbout.C lick += new System.EventHan dler(this.MenuC licks);
//
// mnuExit
//
this.mnuExit.In dex = 10;
this.mnuExit.Te xt = "Exit";
this.mnuExit.Cl ick += new System.EventHan dler(this.MenuC licks);

//
// trayMenu
//
this.trayMenu.M enuItems.AddRan ge(new System.Windows. Forms.MenuItem[]
{
this.mnuAbout,
this.mnuExit});
//
// trayIcon
//
this.trayIcon.C ontextMenu = this.trayMenu;
this.trayIcon.I con =
(System.Drawing .Icon)(resource s.GetObject("tr ayIcon.Icon"));
this.trayIcon.T ext = "TrayIcon Test";
this.trayIcon.V isible = true;
}

private void MenuClicks(obje ct sender, EventArgs e)
{
if (sender == mnuExit) Application.Exi t();
else if (sender == mnuAbout) MessageBox.Show ("About TrayIcon test
program");

}

public static void Main(string[] args)
{
new TrayIcon();
}
}
}

Jul 21 '05 #4
On Thu, 7 Apr 2005 02:49:02 -0700, Lalit wrote:
I have tried this.
I am able to show the Systray icon by setting the allow service to interact
with Desktop property checked.

But the Context menu is not shown for that and i also tried the double click
event of notify icon. If i open the Exe of service directly then context menu
is shown.


I would really advise you to follow what i have told you in my other post.
Showing your tray icon directly from your Windows Service with Interact
with desktop is only going to give you problems. You may be able to get it
working fine when you start the service but as soon as you'll log off or if
2 users are logged in simlutaneously, you'll run into troubles.

Keep in mind that a windows service runs under its own user account (often
Local System) and runs independently of whether there is a (or several)
user logged in or not. If you display any UI element from your Windows
Service, you will have to detect users login and logout (and that's not
that trivial) in order to show/hide your UI elements properly. And under
Windows XP with fast user switching on, you'll probably have headaches
trying to display you tray icon on the desktop of the user that is
currently using the computer since there can be any number of interactive
users logged in simultaneously although only one at a time can effectively
use the computer. And be prepared for Windows 2003 where 2 interactive
users can be logged in and use the computer simultaneously (one at the
console, the other one via Remote Desktop).

Using the approach described in my previous post - that is, having a normal
window form application in the startup folder displaying the tray icon and
communicating with your service via for instance .NET Remoting - allows you
to solve all these problems at once and is fairly easy to implement. This
is how all the Windows Services that need a UI (e.g. antivirus sofwtare)
are implemented.
Jul 21 '05 #5
Hi,

I have no idea of remoting. Another thing i need only one application. If
i use another windows application then i will have two applications to manage.

Regards,
Lalit

"Mehdi" wrote:
On Thu, 7 Apr 2005 02:49:02 -0700, Lalit wrote:
I have tried this.
I am able to show the Systray icon by setting the allow service to interact
with Desktop property checked.

But the Context menu is not shown for that and i also tried the double click
event of notify icon. If i open the Exe of service directly then context menu
is shown.


I would really advise you to follow what i have told you in my other post.
Showing your tray icon directly from your Windows Service with Interact
with desktop is only going to give you problems. You may be able to get it
working fine when you start the service but as soon as you'll log off or if
2 users are logged in simlutaneously, you'll run into troubles.

Keep in mind that a windows service runs under its own user account (often
Local System) and runs independently of whether there is a (or several)
user logged in or not. If you display any UI element from your Windows
Service, you will have to detect users login and logout (and that's not
that trivial) in order to show/hide your UI elements properly. And under
Windows XP with fast user switching on, you'll probably have headaches
trying to display you tray icon on the desktop of the user that is
currently using the computer since there can be any number of interactive
users logged in simultaneously although only one at a time can effectively
use the computer. And be prepared for Windows 2003 where 2 interactive
users can be logged in and use the computer simultaneously (one at the
console, the other one via Remote Desktop).

Using the approach described in my previous post - that is, having a normal
window form application in the startup folder displaying the tray icon and
communicating with your service via for instance .NET Remoting - allows you
to solve all these problems at once and is fairly easy to implement. This
is how all the Windows Services that need a UI (e.g. antivirus sofwtare)
are implemented.

Jul 21 '05 #6
On Thu, 7 Apr 2005 04:25:03 -0700, Lalit wrote:
Hi,

I have no idea of remoting.
It's never too late to learn :-)

So what do you need to tray icon for? Do you just need to display the state
of your Windows Server (started, paused, stopped) and allow the user to
start and stop it? In this case, you don't even need .NET Remoting or any
other cross process communication framework. You can simply use the
ServiceControll er class which is very easy to use.

If you need the user to pass commands to the service, then you need some
way to communicate with the service. You could use raw sockets or named
pipes if you are more familiar with them. If you have never done this sort
of things before then .NET Remoting is you best bet i guess since it's very
easy to learn as long as you are not trying to make overly complicated
things. Have a look on the web, codeproject.com in particular, for some
beginner's tutorials.
Another thing i need only one application. If
i use another windows application then i will have two applications to manage.


That's right, you'll have 2 applications. However, what do you mean by
"managing". Are you thinking of maintenance? In this case, i think that
having 2 applications would be far easier to maintain than having just a
Windows Service with ugly hacks to show a tray icon with context menu in a
reliable way. Having 2 application being the standard way to go to solve
this kind of problem, somebody having to maintain your code will have no
problem understanding what you are doing. If you go the hacking route, then
i hope that you are keeping a very detailled documentation of your code or
it's gonna be a pure horror to maintain.

All that said, if your application is a small app just for yourself, then
you could get away with just a Windows Service showing its tray icon since
you'll know its limitations. I can't help you here since i've never been
able to solve this problem myself (i've never look really hard for a
solution though) but there are surely ways to do whatever you want to do.
If you are developing a "serious" application, then you already know by now
what i think you should do.
Jul 21 '05 #7
Hi,
you are right but i have the problem that the design can not be changed and
i have to provide the solution as i have described. I need to send a command
to the service to process something.

With regards,
Lalit

"Mehdi" wrote:
On Thu, 7 Apr 2005 04:25:03 -0700, Lalit wrote:
Hi,

I have no idea of remoting.


It's never too late to learn :-)

So what do you need to tray icon for? Do you just need to display the state
of your Windows Server (started, paused, stopped) and allow the user to
start and stop it? In this case, you don't even need .NET Remoting or any
other cross process communication framework. You can simply use the
ServiceControll er class which is very easy to use.

If you need the user to pass commands to the service, then you need some
way to communicate with the service. You could use raw sockets or named
pipes if you are more familiar with them. If you have never done this sort
of things before then .NET Remoting is you best bet i guess since it's very
easy to learn as long as you are not trying to make overly complicated
things. Have a look on the web, codeproject.com in particular, for some
beginner's tutorials.
Another thing i need only one application. If
i use another windows application then i will have two applications to manage.


That's right, you'll have 2 applications. However, what do you mean by
"managing". Are you thinking of maintenance? In this case, i think that
having 2 applications would be far easier to maintain than having just a
Windows Service with ugly hacks to show a tray icon with context menu in a
reliable way. Having 2 application being the standard way to go to solve
this kind of problem, somebody having to maintain your code will have no
problem understanding what you are doing. If you go the hacking route, then
i hope that you are keeping a very detailled documentation of your code or
it's gonna be a pure horror to maintain.

All that said, if your application is a small app just for yourself, then
you could get away with just a Windows Service showing its tray icon since
you'll know its limitations. I can't help you here since i've never been
able to solve this problem myself (i've never look really hard for a
solution though) but there are surely ways to do whatever you want to do.
If you are developing a "serious" application, then you already know by now
what i think you should do.

Jul 21 '05 #8

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

Similar topics

3
3034
by: DH | last post by:
I hope this is a right place to post my question I'm working on a Windows service application with VB.net. The purpose is to let the service application as a monitor to periodically check a launching flag file. If the flag was found the service will trigger to run a finance calculating program (VMPE4SRVAPP.exe, a VB 6.0/Access application but without windows interface). I added a Process and two Timers on the Service to do checking and...
2
1365
by: Juan Manuel Alegría B. | last post by:
Hi group, I've sent this question in other group.... How can I see my Windows service application on the task manager. Thanks so much. Guadalajara, Mexico.
2
5434
by: Mustaq | last post by:
Hi I am unable to get Timer1_Tick event in Windows Service Application inspite of setting following things Timer1.enabled = True Timer1.interval = 1000 Let me know how to resolve Thanks &
0
1562
by: Alireza Haghshenass | last post by:
Dear All, I am facing a problem which I could not solve. I am writing an application which uses a notify icon and a context menu bound to it to show modal dialog forms. When these forms is shown there is no problem but while I am closing any of them which is shown in modal form throw an exception which is described below : Message : External component has thrown an exception. StackTrace : at...
5
4272
by: vinoth | last post by:
Hi, I have created WindowsService Project.In that Project OnStart Method i have written the following Code. In this code the Server is waiting for the connection from client. When the Client connects to the Server, the server will process and send result to the client.(This is Client Server Application. The Server side Code is implementd in th OnStart method Of Windows Service). When i tried the Client Server Application in Console...
2
1383
by: Stephajn Craig | last post by:
Is it possible to apply remoting techniques to a Windows Service Application? I have an application that I'm building that is primarily ASP.NET based. However, there are some functions that I want to have run all the time in the background. So I am implementing a Windows service to accomplish this. However, I'd like to expose some funtionality out of this Windows Service as well. I'd like to be able to remote control it to a certain...
1
5756
by: Loane Sharp | last post by:
Hi there Is it possible to set the environment for a windows service application to anything other than "Environment.UserInteractive = False"? Does this (whether I can or can't set the user interactive mode to true) depend on whether the service is running on the local system account or a user account? For instance, when the service is running under the local system account, I can display a message box to the default user using the...
0
1421
by: ld | last post by:
Hi, I have a Windows service that creates a tray icon. the tray icon gets created no problem however I cannot get any of the events on this icon to be fired in the Service. So if you double click on the icon nothing happens. Also I am interested in having a context menu associated to this icon. Is this impossible to do because I am dealing with a service? or is there a workaround?
8
9825
by: =?Utf-8?B?TWlrZVo=?= | last post by:
How Can Windows Service Application Reatarts itself? Thanks.
0
863
by: DavidEngler | last post by:
I'm developing a windows service application in C#, that is logging to a website, and check for a certain file, and if it exist, download it and process it. http://watin.sourceforge.net - works great as a Windows application but doesnt run as windows service. Anyone have an idea how can I make this program work? Thank you
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
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
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
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...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
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
5155
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...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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

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.