Search code examples
c#asp.netwindows-servicesfilesystemwatcher

Keep C# application running


I'm building a Windows Service that uses FileSystemWatcher, and runs in the background.

I don't want to keep on uninstalling and installing the service every time I want to debug, so would like to do most of my development in a normal program before moving it into a service. But I'm quite new to this, and when I run it, it just runs through the block and exits.

What would be a good way to keep the program running?


Solution

  • http://einaregilsson.com/run-windows-service-as-a-console-program/

    I've used this before to debug my service as a Console application based on whether its running in an interactive user environment.

    public partial class DemoService : ServiceBase
    {
        static void Main(string[] args)
        {
            DemoService service = new DemoService();
    
            if (Environment.UserInteractive)
            {
                service.OnStart(args);
                Console.WriteLine("Press any key to stop program");
                Console.Read();
                service.OnStop();
            }
            else
            {
                ServiceBase.Run(service);
            }
        }