我正在写一个小服务,应该记录一些事件。然而,只有其中两个或工作,甚至在搜索之后,我无法弄清楚原因。
这些是我想记录的事件:
我三重检查我的服务可以捕获这些事件(我甚至将所有 CanHandle .. 设置为true ..
private void InitializeComponent()
{
//
// Service1
//
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "TimeToGoService";
}
这是我强调的 OnSessionChange()
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
if (changeDescription.Reason == SessionChangeReason.SessionLock)
{
startBreak();
}
else if (changeDescription.Reason == SessionChangeReason.SessionUnlock)
{
endBreak();
}
base.OnSessionChange(changeDescription);
}
startBreak()和 endBreak()仅将日志写入带有StreamWriter的文件中。
通过在网上搜索,我看到有些人使用隐藏的表单来解雇这些事件。那是问题吗?
答案 0 :(得分:0)
不知道为什么,但上面的代码现在100%工作。可能错过了什么......
编辑:无法确切地确定错误。我可以向我的下一个提供代码旁边的服务配置。
而且......就是这样。请注意,服务不需要与桌面交互以捕获这些事件。
此外,您可以检查您的服务是否从 EventObserver 中的终端获取活动,您将看到它们是否失败或是否成功。
祝你好运,但如果你使用上面的代码和配置,你应该能够使它工作;)