Got this working eventually:
public class MyServiceClass : ServiceControl
{
private readonly Task task;
private HostControl hostControl;
public MyServiceClass()
{
task = new Task(DoWork);
}
private void DoWork()
{
Console.WriteLine("Listen very carefully, I shall say this only once");
//hostControl.Stop();
}
public bool Start(HostControl hostControl)
{
// so we can stop the service at the end of the check
this.hostControl = hostControl;
// start the DoWork thread
task.Start();
return true;
}
public bool Stop(HostControl hostControl)
{
return true;
}
};
and
class Program
{
static void Main(string[] args)
{
HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<MyServiceClass>(serviceConfigurator =>
{
serviceConfigurator.ConstructUsing(() => new MyServiceClass());
serviceConfigurator.WhenStarted((myServiceClass, hostControl) => myServiceClass.Start(hostControl));
serviceConfigurator.WhenStopped((myServiceClass, hostControl) => myServiceClass.Stop(hostControl));
});
hostConfigurator.RunAsLocalSystem();
hostConfigurator.SetDisplayName("MyService");
hostConfigurator.SetDescription("Does stuff.");
hostConfigurator.SetServiceName("MyService");
hostConfigurator.StartAutomatically();
hostConfigurator.EnableShutdown();
});
}
};
My 2nd attempt had a spare "(" at the first mention of serviceConfigurator, then I needed to turn my void Start and Stop functions into bool functions. Hope this helps someone.