Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you are on a service, you can get session change events directly. </p> <p>You can register yourself to receive the <code>SERVICE_CONTROL_SESSIONCHANGE</code> event. In particular, you will want to look for the <code>WTS_SESSION_LOGON</code> and <code>WTS_SESSION_LOGOFF</code> reasons. </p> <p>For details and links to the relevant MSDN docs, check <a href="https://stackoverflow.com/a/16444110/863564">this answer I wrote just yesterday</a>.</p> <p>In C# it is even easier, as ServiceBase already wraps the service control routine and exposes the event as an overridable <code>OnSessionChange</code> method for you. See <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onsessionchange.aspx" rel="nofollow noreferrer">MSDN docs for ServiceBase</a>, and do not forget to set the <code>CanHandleSessionChangeEvent</code> property to true to enable the execution of this method.</p> <p>What you get back when the framework calls your <code>OnSessionChange</code> override is a <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.sessionchangedescription.aspx" rel="nofollow noreferrer">SessionChangeDescription Structure</a> with a reason (logoff, logon, ...) and a session ID you can use to obtain information, for example, on the user logging on/off (see the link to my prev answer for details)</p> <p>EDIT: sample code</p> <pre><code> public class SimpleService : ServiceBase { ... public SimpleService() { CanPauseAndContinue = true; CanHandleSessionChangeEvent = true; ServiceName = "SimpleService"; } protected override void OnSessionChange(SessionChangeDescription changeDescription) { EventLog.WriteEntry("SimpleService.OnSessionChange", DateTime.Now.ToLongTimeString() + " - Session change notice received: " + changeDescription.Reason.ToString() + " Session ID: " + changeDescription.SessionId.ToString()); switch (changeDescription.Reason) { case SessionChangeReason.SessionLogon: EventLog.WriteEntry("SimpleService.OnSessionChange: Logon"); break; case SessionChangeReason.SessionLogoff: EventLog.WriteEntry("SimpleService.OnSessionChange Logoff"); break; ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload