C# Windows 服务无法将关机事件记录到远程 API
C# Windows Service Fails To Log Shutdown Event to Remote API
我正在生成一个 C# windows 服务来记录特定的 windows 事件。虽然它们中的大多数都很好(Logon/off、Lock/Unlock 等),但我在 Windows 断电类型事件方面遇到了一些问题。我正在尝试的代码如下所示(摘自其他地方的其他建议)。
对于服务:-
public Service()
{
InitializeComponent();
SystemEvents.SessionEnded += new SessionEndedEventHandler(SystemEvents_SessionEnded);
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}
对于SystemEvents_SessionEnded:-
void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e)
{
switch (e.Reason)
{
case SessionEndReasons.SystemShutdown:
eventType = 7;
break;
case SessionEndReasons.Logoff:
eventType = 2;
break;
default:
break;
}
\Send eventType and current DateTime to remote API
}
而 SystemEvents_PowerModeChanged
static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Suspend:
eventType = 5;
break;
case PowerModes.Resume:
eventType = 6;
break;
default:
break;
}
\Send eventType and current Datetime to API
}
由于 API 在本地机器外部托管,我假设我的服务依赖于其他人通过网络发送数据?关于是我的代码失败了还是我是否需要更好的依赖项设置,有什么建议吗?
提前致谢。
This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class.
我正在生成一个 C# windows 服务来记录特定的 windows 事件。虽然它们中的大多数都很好(Logon/off、Lock/Unlock 等),但我在 Windows 断电类型事件方面遇到了一些问题。我正在尝试的代码如下所示(摘自其他地方的其他建议)。
对于服务:-
public Service()
{
InitializeComponent();
SystemEvents.SessionEnded += new SessionEndedEventHandler(SystemEvents_SessionEnded);
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}
对于SystemEvents_SessionEnded:-
void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e)
{
switch (e.Reason)
{
case SessionEndReasons.SystemShutdown:
eventType = 7;
break;
case SessionEndReasons.Logoff:
eventType = 2;
break;
default:
break;
}
\Send eventType and current DateTime to remote API
}
而 SystemEvents_PowerModeChanged
static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Suspend:
eventType = 5;
break;
case PowerModes.Resume:
eventType = 6;
break;
default:
break;
}
\Send eventType and current Datetime to API
}
由于 API 在本地机器外部托管,我假设我的服务依赖于其他人通过网络发送数据?关于是我的代码失败了还是我是否需要更好的依赖项设置,有什么建议吗?
提前致谢。
This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class.