Not 100% au fait with windows services, I have the following:
protected override void OnStart(string[] args)
{
_myManager.Start();
}
In MyManager:
public void Start()
{
_myTask = Task.Run(DoStuff);
}
In theory then, the OnStart method will return and the service will launch successfully. async Task DoStuff()
is just an infinite loop (it will exit if the service is stopped though).
I want to add try/catch to this. Ideally it would be in the OnStart method, so that I can call this.Stop()
to stop the service (since it will be the ServiceBase
). This is to catch any exceptions I haven't explicitly handled, so I can log them and shut down the service.
Can I simply wrap a try catch around _myManager.Start();
? Or do I need to do something clever involving tasks?