Ich habe versucht, den Sitzungsschalter zu abonnieren. Es wird erfolgreich abonniert. Aber das Ereignis löst nicht aus. < /P>
Code: Select all
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
namespace MyFirstService
{
public partial class Service1 : ServiceBase
{
private const int SERVICE_CONTROL_SESSIONCHANGE = 0x0000000D;
private const int WTS_SESSION_LOGON = 0x5;
private const int WTS_SESSION_LOGOFF = 0x6;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteToFile("Service started at " + DateTime.Now);
}
protected override void OnStop()
{
WriteToFile("Service stopped at " + DateTime.Now);
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
switch (changeDescription.Reason)
{
case SessionChangeReason.SessionLogon:
WriteToFile("User logged on at " + DateTime.Now);
break;
case SessionChangeReason.SessionLogoff:
WriteToFile("User logged off at " + DateTime.Now);
break;
case SessionChangeReason.SessionLock:
WriteToFile("Workstation locked at " + DateTime.Now);
break;
case SessionChangeReason.SessionUnlock:
WriteToFile("Workstation unlocked at " + DateTime.Now);
break;
default:
WriteToFile($"Session change event: {changeDescription.Reason}");
break;
}
}