views:

221

answers:

2

When an application is running as a Windows Service (written in .NET), what would be the best way to detect whether or not a user is logged on, and when a user logs on, to be able to begin displaying the necessary dialogs, or when that user has logged off, so the display of dialogs can be prevented?

I hope I phrased this correctly :)

+4  A: 

You shouldn't be doing this at all from a service. First, a service by default doesn't have access to the desktop. You can change this in versions of Windows before Vista by checking the "Allow service to interact with desktop" checkbox on the properties dialog for the service, but this is a very, very bad idea.

In Vista, services cannot interact with the desktop at all.

You are making the assumption that there will only ever be one interactive user on the machine at the time, which is not always the case. Because of Terminal Services, you can have more than one user logged into interactive sessions.

What you should do is create a small client program which is launched when the user logs in. This program would interact with the service through some distributed technology (like Remoting, or WCF, I recommend the latter). Based on the communication it receives from the server, that client program can launch dialogs and user interface elements to interact with the user.

casperOne
I really wanted to avoid having two separate processes running to achieve this, but I guess it is the only way :(
hmcclungiii
@hmcclungiii: Don't let it get you down, this is the ^right^ way, and that's what's more important.
casperOne
A: 

You won't be able to show any UI from your service whether or not the user is logged in. The service will be running in a different desktop for security reasons - it is possible to get around this, but please don't. The best thing to do is to have a separate program that will show your UI and then use some form of interprocess communication between the two.

Stu Mackellar