Windows 服务:会话解锁事件与快速用户切换和终端服务停止和禁用

发布于 2024-11-19 20:22:34 字数 1227 浏览 7 评论 0原文

我正在编写一个 C# .NET 3.5 Windows 服务,每当用户登录或解锁事件发生时,该服务都需要执行一些操作。我尝试通过将事件处理程序添加到 Microsoft.Win32.SystemEvents.SessionSwitch 来注册服务:

using Microsoft.Win32;

SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);

void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
    if (e.Reason == SessionSwitchReason.SessionLogon)
    { 
        //  Do Task
    }
    else if (e.Reason == SessionSwitchReason.SessionUnlock)
    { 
        //  Do Task
    }
}

此外,我还尝试重写 OnSessionChange(SessionChangeDescription changeDescription) {...} 继承的方法>ServiceBase 类:

protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
    if (changeDescription.Reason == SessionChangeReason.SessionLogon)
    {
        //  Do Task
    }
    else if (changeDescription.Reason == SessionChangeReason.SessionUnlock)
    {
        //  Do Task
    }

    base.OnSessionChange(changeDescription);
}

许多会话事件由所描述的任一方法处理,不幸的是,当快速用户切换和终端服务停止和禁用时,这些方法都无法处理会话解锁事件。然而,当两个服务都启用并运行时,该事件就会被处理。将部署的工作环境不会启用这些服务。

是否有另一种方法可以在 C# .NET 托管代码环境中完成此任务?我见过的许多问题都用上述方法回答了问题,它们确实可以正常工作,但在快速用户切换和终端服务都被禁用时却无法正常工作。

I am writing a C# .NET 3.5 Windows Service that needs to perform some actions whenever a user logon or unlock event occurs. I have tried registering the service by adding my event handler to Microsoft.Win32.SystemEvents.SessionSwitch:

using Microsoft.Win32;

SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);

void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
    if (e.Reason == SessionSwitchReason.SessionLogon)
    { 
        //  Do Task
    }
    else if (e.Reason == SessionSwitchReason.SessionUnlock)
    { 
        //  Do Task
    }
}

Also I have tried to override the OnSessionChange(SessionChangeDescription changeDescription) {...} method inherited by the ServiceBase class:

protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
    if (changeDescription.Reason == SessionChangeReason.SessionLogon)
    {
        //  Do Task
    }
    else if (changeDescription.Reason == SessionChangeReason.SessionUnlock)
    {
        //  Do Task
    }

    base.OnSessionChange(changeDescription);
}

Many session events are handled by either of the methods described, unfortunately neither of these methods handle the event of a session unlock when fast user switching and terminal services are stopped and disabled. However the event is handled when both services are enabled and running. The work environment this will be deployed on will not have the services enabled.

Is there another way to accomplish this within the C# .NET managed code environment? Many questions I have seen answer the question with the methods described above, they do work correctly but not when both fast user switching and terminal services is disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2024-11-26 20:22:35

CanHandleSessionChangeEvent 属性默认设置为 False

初始化组件时插入以下代码

public Service1()
        {
            InitializeComponent();
            this.CanHandleSessionChangeEvent = true;
            this.CanHandlePowerEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;
            this.AutoLog = true;
        }

The CanHandleSessionChangeEvent property is set to False by default.

Insert the following code when initializing the components

public Service1()
        {
            InitializeComponent();
            this.CanHandleSessionChangeEvent = true;
            this.CanHandlePowerEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;
            this.AutoLog = true;
        }
神回复 2024-11-26 20:22:35

如果您在使用 SessionSwitchReason 时遇到困难,您可以在 Windows 事件日志中启用“其他登录/注销事件”并自行监视这些事件。这有点混乱,因为您必须以某种方式轮询日志,但它确实有效。

请参阅 - https://stackoverflow.com/a/40675839/6715034

If you're having difficulty with SessionSwitchReason you can enable 'Other Logon/Logoff Events' in windows eventlog and monitor for these events yourself. It's a bit messy because you have to poll the logs in someway, but it does the trick.

See - https://stackoverflow.com/a/40675839/6715034

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文