Run an executable as a background service

In Compact Framework 2.0, I want to run a 'console application' as a background service.

Here is the code I have:


class Program { static void Main(string[] args) { BackgroundTasks bt = new BackgroundTasks(); while (true) { System.Threading.Thread.Sleep(10000); } } }
 


I just want the object of BackgroundTasks to run forever (listening for events), but I feel that causing the main thread to keep sleeping isn't the most efficient way

And yes, I can do this nativally with an extension to services.exe, but I want managed :)

Thanks in advance, Andrew


Answer this question

Run an executable as a background service

  • BruceWang.Korea

    Thanks Tim, that is a pretty elegant way to do it!

    Kind regards, Andrew

  • RobF

    FWIW, getting any thread to go to sleep from time to time is a good way to preserve battery life on a device
  • Methark

    Create a syncronization object (System.Threading.AutoResetEvent). After creating your BackgroundTasks object, just wait on this event. If your BackgroundTasks object ever does decide it needs to exit, you can signal the event and your main thread will exit too.

  • Run an executable as a background service