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.
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.
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
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).
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