Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a system tray icon for C# window service.?

Tags:

c#

windows

How can I display a system tray icon for C# window service.?

like image 223
Ashish Ashu Avatar asked Apr 16 '10 10:04

Ashish Ashu


People also ask

How do I display system tray icons?

Press the Windows key , type "taskbar settings", then press Enter . Or, right-click the taskbar, and choose Taskbar settings. In the window that appears, scroll down to the Notification area section. From here, you can choose Select which icons appear on the taskbar or Turn system icons on or off.

Where is the system tray icon located?

The notification area (also called the "system tray") is located in the Windows Taskbar, usually at the bottom right corner. It contains miniature icons for easy access to system functions such as antivirus settings, printer, modem, sound volume, battery status, and more.


2 Answers

Services run in a different window station than the logged in user, so you can't have a system tray icon for them. From https://docs.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications:

Windows Service applications run in a different window station than the interactive station of the logged-on user. A window station is a secure object that contains a Clipboard, a set of global atoms, and a group of desktop objects. Because the station of the Windows service is not an interactive station, dialog boxes raised from within a Windows service application will not be seen and may cause your program to stop responding. Similarly, error messages should be logged in the Windows event log rather than raised in the user interface.

The Windows service classes supported by the .NET Framework do not support interaction with interactive stations, that is, the logged-on user. The .NET Framework also does not include classes that represent stations and desktops. If your Windows service must interact with other stations, you will need to access the unmanaged Windows API. For more information, see the Windows SDK documentation.

The interaction of the Windows service with the user or other stations must be carefully designed to include scenarios such as there being no logged on user, or the user having an unexpected set of desktop objects. In some cases, it may be more appropriate to write a Windows application that runs under the control of the user.

Here are a couple of links about how to write to the system tray. You'll need another application to interface with the service, since the service can't directly have an icon in the system tray.

How can I make a .NET Windows Forms application that only runs in the System Tray?

and

http://msdotnetsupport.blogspot.com/2008/02/cnet-application-windows-system-tray.html

like image 105
kemiller2002 Avatar answered Sep 20 '22 11:09

kemiller2002


You can't, not directly, because the windows service will necessarily start when the machine does, not when a user logs in. The service will also be running in a different context, likely as a different user.

What you can do is to write a separate system tray based "controller" that interacts with the service.

like image 30
Bevan Avatar answered Sep 19 '22 11:09

Bevan