A Service determining whether a user is logged on

Go To StackoverFlow.com

1

I have a .NET application with both a Service and an application. I ask the service to alert me when the application is not running and vice versa. This is running on over 1000 machines with different logon credentials (normal login user names, ie bob, sam, fred for machine 1-PC, 2-PC, 3-PC).

I want to know when the application is not running so I can address it, however when the computer reboots and waits at the login screen I am constantly being told my app is not running, but actually there is no one logged in. Is there a way to see if a user is logged in? I have seen a few hacks but nothing concrete/no elegant way to achieve this.

That way I will be able to see the service is running, but not expect the application to be running until someone has logged onto the machine.

2012-04-04 01:16
by Scottf007
Do you expect your application to run the entire time someone is logged in? Should it be expected that the application would restart the service if it would stop (kind of like what the viruses that install services do) - M.Babcock 2012-04-04 01:33
The application stays logged in - sits in the taskbar. An application can not restart a service as far as I am aware. We have not had problems with the service stopping - Scottf007 2012-04-04 01:56
Applications can absolutely stop and start services - Jesse 2015-06-18 14:15


0

If your service is rock solid (i.e. rarely crashes), what I would do is write a temporary value somewhere (file, registry, database) when the service starts.

Then, the first time you see the application running clear the flag.

Thereafter, if the flag is not set and the application is not running, there is an issue.

This approach does not solve the case where the user logs off, only the reboot scenario, so I am not 100% sure this will work for you, but you only mention the reboot scenario, so I am hoping this may give you some ideas.

2012-04-04 01:25
by competent_tech
Thanks competent_tech, I can see if the application is running by seeing if the process is running, which works. The problem as you eluded to is in situations where it is not running, but also should not be running. I need to know when there is a problem vs just a status - Scottf007 2012-04-04 01:58
Do you have control over the application? If so, I would set a flag somewhere when it starts and then, when it exits gracefully, clear the flag. In your service, if the flag is set and the app is not running, then there is a problem. To handle reboots, always clear the flag when the service is started - competent_tech 2012-04-04 02:20


0

You could use a Global\MYUniqueEvent Object inside your Application and provide the necessary security credentials on that so that your Service can Check for it.

You can also possibly listen for Microsoft.Win32.SystemEvents.SessionSwitch to determine that a user is logging in. (I haven't personally used that, but Know it's there). Have a look at this for an example http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.asp

2012-04-04 01:28
by Paul Farry


0

This couple links can be useful

Note: there is a good chance that there is much better built in way of doing so - serverfault maybe a better site to ask question about managing large number of machines (unless you do shady activity with you tools).

2012-04-04 01:38
by Alexei Levenkov
Hi Alexi it looks like checking who is running explorer.exe is the best bet so far. I am going to implement it and see how it work - Scottf007 2012-04-11 12:31


0

var ntuser = new NTAccount(UserName);

var securityIdentifier = (SecurityIdentifier)ntuser.Translate(typeof(SecurityIdentifier));

var okey = Registry.Users.OpenSubKey(securityIdentifier + @"\Control Panel\Desktop", true); //any key is ok

if (okey != null)
//login
else
//not login
2013-05-09 09:07
by wzpchina