C# 在windows services中无法添加会话结束事件
由于最近下班老是忘记打卡,我想写一个关机前提醒我打卡的弹窗,我用控制台在SystemEvents.SessionEnding里添加事件,能很好的运行,但是我把他做成服务后,发现失效了,里面的弹窗无法运行,SessionEndingEventArgs.Cancel也无法取消关机。请问这是什么原因呢。
代码如下:
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
this.CanShutdown = true;
this.CanHandleSessionChangeEvent = true;
}
protected override void OnStart(string[] args)
{
Debugger.Launch();
SystemEvents.SessionEnding += MyEvent;
}
private void MyEvent(object sender, SessionEndingEventArgs e)
{
Interop.ShowMessageBox(e.Reason.ToString(), "title");
}
protected override void OnStop()
{
}
protected override void OnShutdown()
{
base.OnShutdown();
}
}