Capture Terminal Session Lock and Unlock events in C# Windows Service

Posted on 2008-10-15
Last Modified: 2013-12-17
I need to create a Windows Service that will capture when a user locks and unlocks their desktop session.  Also, I need to capture the username.  I know how to do this using a regular application, just not a Windows Service.
Question by:fcti
  • 2
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22727675
have a look at the following ee link - i think it might be helpful

Author Comment

ID: 22727698
Sorry Ragi, that solution won't apply, its for a Windows Application, not a Service.  It relies on WndProc.

Accepted Solution

fcti earned 0 total points
ID: 22735758
Found the solution.  I am overriding the following event in my service:

protected override void OnSessionChange(SessionChangeDescription changeDescription)


switch (changeDescription.Reason)


                case SessionChangeReason.SessionLogon:



                case SessionChangeReason.SessionLogoff:



                case SessionChangeReason.RemoteConnect:

                    //Remote Connect


                case SessionChangeReason.RemoteDisconnect:

                    //Remote Disconnect


                case SessionChangeReason.SessionLock:



                case SessionChangeReason.SessionUnlock:







Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now