Search code examples
c#windowsservicetimer

C# Windows Service - My timer is not working, getting multiple threads


I have written a service to check whether an application is running, and it launch it if not. The timer should have the service running every 30 seconds.

My code is not working properly. It will launch multiple threads upon start up, then stops and never starts another instance, it just sits there.

I need it to launch one thread, wait 30 seconds, then start another.

namespace ProcessRestart
{
    public partial class Service1 : ServiceBase
    {
        static string _exeName = ConfigurationManager.AppSettings["_exeName"].ToString();
        static string _exePath = ConfigurationManager.AppSettings["_exePath"].ToString();
        static string _logFilePath = ConfigurationManager.AppSettings["_logFilePath"].ToString();
        StringBuilder sb = new StringBuilder();
        Timer timer = new Timer();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            sb.AppendLine(DateTime.Now + " Service started up.");
            System.IO.File.AppendAllText(_logFilePath, sb.ToString());

            CheckProcess();
            timer.Interval = 30000; // 30 seconds
            timer.Enabled = true;
            timer.Start();

            sb.AppendLine(DateTime.Now + " Timer set to " + timer.Interval);
            System.IO.File.AppendAllText(_logFilePath, sb.ToString());
        }

        protected override void OnStop()
        {
            timer.Stop();
            timer.Dispose();
            timer = null;
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            sb.AppendLine(DateTime.Now + " Service recalled.");
            System.IO.File.AppendAllText(_logFilePath, sb.ToString());
        }

        private void CheckProcess()
        {
            Process[] processName = Process.GetProcessesByName(_exeName);

            sb.AppendLine(DateTime.Now + " Number of instances of " + _exeName + " running:" + processName.Length);
            System.IO.File.AppendAllText(_logFilePath, sb.ToString());

            if (processName.Length <= 0) // Process is not running, we need to start it up
            {
                sb.AppendLine(DateTime.Now + " " + _exeName + " is not running, so starting it up now.");
                System.IO.File.AppendAllText(_logFilePath, sb.ToString());
                Process.Start(_exePath);
            }
            else
            {
                sb.AppendLine(DateTime.Now + " " + _exeName + " is already running.");
                System.IO.File.AppendAllText(_logFilePath, sb.ToString());
            }
        }
    }
}

The logging looks like this:

4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Number of instances of process running: 1
4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Number of instances of process running: 1
4/14/2021 2:52:42 PM Process is already running.
4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Number of instances of process running: 1
4/14/2021 2:52:42 PM Process is already running.
4/14/2021 2:52:42 PM Timer set to 30000

Anyone know what I'm doing wrong?

Thanks.


Solution

  • CheckProcess(); should be invoked in the OnElapsedTime not in the OnStart.