我得到了下面的代码从http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/如何时通知服务站要求
当在控制台模式下运行,我想通知当请求服务停止,而不是等待用户输入。 (我的意思是在这里用户请求通过Ctrl + C或关闭控制台停止程序)
当作为服务工作时,OnStop被停止请求调用是微不足道的,但我怎样才能实现一种解决方法,以便我也可以在控制台模式下工作时会收到通知。
那么是否有任何事件可以订阅通知或任何成员函数等?
在此先感谢。
最好的问候,
-Victor
using System;
using System.ServiceProcess;
public partial class DemoService : ServiceBase
{
static void Main(string[] args)
{
DemoService service = new DemoService();
if (Environment.UserInteractive) // Console mode
{
service.OnStart(args);
Console.WriteLine("Press any key to stop program");
Console.Read();
service.OnStop();
}
else
{
ServiceBase.Run(service);
}
}
public DemoService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down
//necessary to stop your service.
}
}
我想你可以使用类似看门狗的模式。以前没有用过这个东西。 – Warty 2010-08-13 11:53:27