As a rule, with services, the task that you want to complete is repeated, perhaps in a cycle or, possibly, in a trigger or, possibly, in another.
I use Topshelf to do the second task for me, in particular, I use the Shelf'ing functionality.
The problem I am facing is how to handle the task loop.
When you load a service into Topshelf, you pass it a class (in this case, ScheduleQueueService ) and indicate that it is its Start method and Stop method:
Example:
public class QueueBootstrapper : Bootstrapper<ScheduledQueueService> { public void InitializeHostedService(IServiceConfigurator<ScheduledQueueService> cfg) { cfg.HowToBuildService(n => new ScheduledQueueService()); cfg.SetServiceName("ScheduledQueueHandler"); cfg.WhenStarted(s => s.StartService()); cfg.WhenStopped(s => s.StopService()); } }
But in my StartService() method, I use a while loop to repeat the task that I'm starting, but when I try to stop the service through Windows services, it does not stop, and I suspect it because the StartService() method never ended when it was originally called.
Example:
public class ScheduledQueueService { bool QueueRunning; public ScheduledQueueService() { QueueRunning = false; } public void StartService() { QueueRunning = true; while(QueueRunning){
What is the best way to do this?
I looked at using the .NET System.Threading.Tasks to start work, and then possibly closing the thread in StopService ()
Perhaps using Quartz to repeat the task and then delete it.
Thoughts?
source share