检测停机窗口服务窗口

2023-09-03 11:05:53 作者:风华正茂╰

我有获得用户的详细信息,并将结果保存到日志文本文件中的窗口服务。并且,我的问题是,当我关闭或注销我的系统,我也想的时候,我从我的系统保存到该日志文件。但是,我不知道该怎么做。

我查了winproc方法来检测关机操作,但是我没能使用它的窗口服务,在Google上搜寻发现它只能与形式使用。我们如何能够检测用户单击了关闭或注销,并做一些动作。 所以,请给我一些意见或建议上。

我已经用了注销,但在日志条目当我注销系统是由

 保护覆盖无效OnSessionChange(SessionChangeDescription changeDescription)
  {
     this.RequestAdditionalTime(250000); //给出注销25秒的延迟
     如果(changeDescription.Reason == SessionChangeReason.SessionLogoff)
     {
        //添加您节省code在这里
        StreamWriter的海峡=新的StreamWriter(D:\\ Log.txt文件,真正的);
        str.WriteLine(服务采空因,上+ DateTime.Now.ToString + changeDescription.Reason.ToString()+());
        str.Close();
     }
     base.OnSessionChange(changeDescription);
 }
 

解决方案

有关关机,覆盖OnShutdown方式:

 保护覆盖无效OnShutdown()
{
    //你的code在这里
    base.OnShutdown();
}
 
小窗口传递大文明丨金华市中心医院开展检验窗口服务礼仪培训

有关注销:

首先,在服务构造一个事件处理程序添加到Microsoft.Win32.SystemEvents.SessionEnded:

 公共则将MyService()
{
    的InitializeComponent;
    Microsoft.Win32.SystemEvents.SessionEnded + =新Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
}
 

然后添加处理程序:

 无效SystemEvents_SessionEnded(对象发件人,Microsoft.Win32.SessionEndedEventArgs E)
{
    //你的code在这里
}
 

这应该抓住任何端会话,包括控制台本身(运行服务)。

i have a windows service that get user details and save the result into log text file. and, my problem is when i shut down or log off my system, i also would like to save the time that i down my system into that log file. but, i don't know how to do that.

I checked the winproc method to detect shutdown operation but i was not able to use it on window service, on googling found it can be used with forms only. how can we detect user have clicked shutdown or log off and do some action. so,please give me some idea or suggestion on that.

i have used it for logoff but on log entry is made when i logoff the system

  protected override void OnSessionChange(SessionChangeDescription changeDescription)
  {
     this.RequestAdditionalTime(250000); //gives a 25 second delay on Logoff
     if (changeDescription.Reason == SessionChangeReason.SessionLogoff)
     {
        // Add your save code here
        StreamWriter str = new StreamWriter("D:\\Log.txt", true);
        str.WriteLine("Service stoped due to " + changeDescription.Reason.ToString() + "on" + DateTime.Now.ToString());
        str.Close();
     }
     base.OnSessionChange(changeDescription);
 }

解决方案

For a shutdown, override the OnShutdown method:

protected override void OnShutdown()
{
    //your code here
    base.OnShutdown();
}

For a logoff:

First, add an event handler to Microsoft.Win32.SystemEvents.SessionEnded in the Service Constructor:

public MyService()
{
    InitializeComponent;
    Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
}

Then add the handler:

void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
{
    //your code here
}

This should catch any ended session, including the console itself (the one running the services).