Hello all, I got below code from http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/
When running in console mode, I would like to be notified when service is requested to stop, rather than waiting user input. ( I mean here user requested to stop program via Ctrl+C or by closing console)
It is trivial that when working as a service OnStop is called upon stop request, but how can I implement a workaround so that I can also be notified when working in console mode.
So is there any event that I can subscribe to be notified or any member function etc.?
Thanks in advance.
Best regards,
-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.
}
}