Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Determine a Duration of a Locked Workstation?

Tags:

c#

windows

How can one determine, in code, how long the machine is locked?

Other ideas outside of C# are also welcome.


I like the windows service idea (and have accepted it) for simplicity and cleanliness, but unfortunately I don't think it will work for me in this particular case. I wanted to run this on my workstation at work rather than home (or in addition to home, I suppose), but it's locked down pretty hard courtesy of the DoD. That's part of the reason I'm rolling my own, actually.

I'll write it up anyway and see if it works. Thanks everyone!

like image 695
AgentConundrum Avatar asked Sep 04 '08 23:09

AgentConundrum


Video Answer


1 Answers

I hadn't found this before, but from any application you can hookup a SessionSwitchEventHandler. Obviously your application will need to be running, but so long as it is:

Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);  void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) {     if (e.Reason == SessionSwitchReason.SessionLock)     {          //I left my desk     }     else if (e.Reason == SessionSwitchReason.SessionUnlock)     {          //I returned to my desk     } } 
like image 80
Timothy Carter Avatar answered Sep 23 '22 22:09

Timothy Carter