[英]How to make a .NET Windows Service detect Logon, Logoff and Switch User events?
[英]How to reliably capture Windows logon, logoff, lock and unlock events from a service?
using Microsoft.Win32;
public class App
{
static void Main()
{
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
Console.ReadLine();
SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
}
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if(e.Reason == SessionSwitchReason.SessionLock)
{
Console.WriteLine("locked at {0}", DateTime.Now);
}
if(e.Reason == SessionSwitchReason.SessionUnlock)
{
Console.WriteLine("unlocked at {0}", DateTime.Now);
}
}
我已經創建了Windows服務。 當我重新啟動系統並登錄,注銷,鎖定或解鎖會話時。 它不會捕獲事件。 該服務正在運行,但無法正常運行。
當我重新啟動服務時,它將按預期捕獲所有事件。 如何進行故障排除/修復?
嘗試保留事件的注冊狀態,而不是在readline之后將其刪除。您正在注冊以偵聽,然后立即取消注冊,這沒有任何意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.