0

I'm creating a windows service (my first one) that checks database regularly using a timer and converts video files if there are any queued. But when I start service it just exits as there is nothing to do at the moment. what can I do to put service in a kind of Idle mode that just stays running?

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • well `while (true)` will be infinite loop my timer will never be triggered, I think there must be a better way – Ashkan Mobayen Khiabani Sep 09 '14 at 15:54
  • possible duplicate of [Windows Service to run constantly](http://stackoverflow.com/questions/4864673/windows-service-to-run-constantly) – eddie_cat Sep 09 '14 at 15:57
  • nope Windows [Service to run constantly](http://stackoverflow.com/questions/4864673/windows-service-to-run-constantly) is not the answer because it doesn't exit until there is nothing to do. I want to keep the service alive even if there is nothing to do. – Ashkan Mobayen Khiabani Sep 09 '14 at 16:06
  • I don't think you read the answers. Checking the database is "something to do," it's your timer event. You just have to have a way to make sure your program lives until the next timer event is fired. – eddie_cat Sep 09 '14 at 16:08

1 Answers1

1

You can do something like this..

OnStart()
{
  while(!serviceNotStopped)
  {
   <do stuff, create threads >
    <Sleep if required>
  }

 <stop signal for all child thread if any >
}




 OnStop()
 {
  serviceNotStopped = true;
 }
NMK
  • 1,010
  • 10
  • 18
  • 1
    This is the *wrong* way to do it. The `OnStart` method is a callback function from the Service Control Manager, and it needs to return in a timely fashion. Typically, you'd start a timer or kick off a thread to do the work of the service. – Matt Davis Sep 09 '14 at 17:14