Windows 服务:会话解锁事件与快速用户切换和终端服务停止和禁用
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CanHandleSessionChangeEvent
属性默认设置为False
。初始化组件时插入以下代码
The
CanHandleSessionChangeEvent
property is set toFalse
by default.Insert the following code when initializing the components
如果您在使用
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