如何使用C#.NET创建预关闭窗口服务? [英] How to create pre shutdown window service using C# .NET?

查看:85
本文介绍了如何使用C#.NET创建预关闭窗口服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想在c#中使用Windows服务运行exe预关机系统。



但是当用户点击关机按钮并在关机前首先运行服务或exe时,怎么可能呢。



我不想在运行前关机exe。



请帮帮我。



先谢谢。



Ankit Agarwal

软件工程师



我的尝试:



Hello,

I want to run a exe pre shutdown system using windows service in c#.

But how it can be possible, when user click on shutdown button and first run the service or exe before shutdown.

I don't want to shutdown before run exe.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

Timer timer1 = new Timer();
       protected override void OnStart(string[] args)
       {
           try
           {
               //Debugger.Break();
               TraceService("Start Service" + " " + DateTime.Now.ToString());
               //timer1.Interval = 1000;

               timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
               timer1.Enabled = true;
               //Debugger.Break();
           }
           catch (Exception ex)
           {
               TraceService("Error: " + ex);
               //EventLog.WriteEntry("Error: " + ex.Message);
           }
       }

       private void OnElapsedTime(object source, ElapsedEventArgs e)
       {
           try
           {

               //timer1.Stop();
               //Debugger.Break();

               timer1.Enabled = false;
               TraceService("Another entry at" + " " + DateTime.Now);
               //TraceService("System shutdown Sucessfully" + " " + DateTime.Now.ToString());
               timer1.Enabled = true;
               //WindowsServiceSchedular(timer1);
           }
           catch (Exception ex)
           {
               EventLog.WriteEntry("Error: " + ex.Message);
               TraceService("Error: " + ex);
           }
       }
       private void TraceService(string content)
       {
           try
           {

               if (!Directory.Exists(@"C:\SystemShutdown\SystemShutdownLogFile"))
                   Directory.CreateDirectory(@"C:\SystemShutdown\SystemShutdownLogFile");
               FileStream fs = new FileStream(@"C:\SystemShutdown\SystemShutdownService.Txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
               StreamWriter sw = new StreamWriter(fs);
               sw.BaseStream.Seek(0, SeekOrigin.End);
               sw.WriteLine(content);
               sw.Flush();
               sw.Close();
           }
           catch (Exception ex)
           {
               EventLog.WriteEntry("Error: " + ex.Message);
           }
       }
       protected override void OnShutdown()
       {
           TraceService("System shutdown");
           //I want to run exe in this function but firstly message is not coming when i am shutting down the computer.
           base.OnShutdown();
       }
       protected override void OnCustomCommand(int command)
       {
           base.OnCustomCommand(command);
       }
       protected override void OnStop()
       {
           timer1.Stop();
           TraceService("Stop Service");
       }

推荐答案

检查出来 - .net framework - C#windows 7在计算机关闭之前运行exe - 超级用户 [ ^ ]

Check this out - .net framework - C# windows 7 run exe before computer shutdown - Super User[^]
Quote:

启动关闭时启动进程是一个馊主意。在这个阶段,最可能的结果是找回错误1115 ERROR_SHUTDOWN_IN_PROGRESS。



更好的方法是安装服务并注册服务以通过错误接收关闭通知RegisterServiceCtrlHandlerEx,请参阅关闭:



服务应用程序在其处理程序例程中接收关闭通知。要注册服务控制处理程序,请使用RegisterServiceCtrlHandlerEx函数。



C#服务可以通过将CanShutdown设置为true然后处理OnShutdown事件来完成此操作。重要的是成为服务而不是应用程序,因为在没有会话登录时可能发生关闭和/或在关闭发生时可能正在运行多个会话。

Launching a process when the shutdown was initiated is a bad idea. At this stage the most likely outcome is to get back the error 1115 ERROR_SHUTDOWN_IN_PROGRESS.

A much better way is to install a service and register the service to receive shutdown notifications via RegisterServiceCtrlHandlerEx, see shutting down:

Service applications receive shutdown notifications in their handler routines. To register a service control handler, use the RegisterServiceCtrlHandlerEx function.

A C# service would do this by setting CanShutdown to true and then handling the OnShutdown event. It is important to be service, not an application, as shutdown can occur when no session is logged on and/or multiple sessions could be running when the shutdown occurs.


这篇关于如何使用C#.NET创建预关闭窗口服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆