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

2

47

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

Reardon answered 16/4, 2010 at 10:44 Comment(0)
S
43

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://learn.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

Selfdeceit answered 16/4, 2010 at 10:49 Comment(0)
R
16

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.

Rehearing answered 16/4, 2010 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.