-1

My service is running but it does not do any of the logic i inserted in OnStart, Also, I am try to log messages to the eventviewer but it does not even get to that part (which i assume is falling before the OnStart..) Can someone please tell me what i am doing wrong? Thanks,

 public partial class DSGService : ServiceBase
    {
        private static string ftpPath = ConfigurationManager.AppSettings["FtpPath"];
        private Timer _timer;
        private bool _isRunning;

        public DSGService()
        {
            InitializeComponent();
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            WriteToEventViewer("error!:" + e.ExceptionObject.ToString());
        }

OnStart logic

        protected override void OnStart(string[] args)
        {
            _isRunning = false;
            _timer = new Timer
            {
                AutoReset = true,
                Interval = 5000,

            };
            _timer.Elapsed += new ElapsedEventHandler(this.TimerElapsed);
            _timer.Start();
        }

code to do things

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (_isRunning)
            {
                WriteToEventViewer("isrunning is true");
                return;
            }

            try
            {
                _isRunning = true;
                WriteToEventViewer("started");
                //do things..
                    WriteToEventViewer("generated!");
                }

            }
            finally
            {
                _isRunning = false;
                WriteToEventViewer("done");
            }
        }
        private void GenerateAndPublishData(ServerData server)
        {
            try
            {
//do things...

            }
            catch (Exception ee)
            {
                WriteToEventViewer(string.Format("error: {0}", ee);
            }
        }
        protected override void OnStop()
        {

            _timer.Stop();
            _timer.Dispose();
            _timer = null;
                    }

write to eventviewer

        private void WriteToEventViewer(string msg)
        {
            using (EventLog eventLog = new EventLog("DSGService"))
            {
                eventLog.Source = "DSGService";
                eventLog.WriteEntry(msg, EventLogEntryType.Information, 101, 1);
            }
        }
    }```
idoW
  • 29
  • 1
  • 9
  • 4
    Does this answer your question? [How to start method on timer from windows service?](https://stackoverflow.com/questions/43763321/how-to-start-method-on-timer-from-windows-service) – oleksa Jan 14 '20 at 09:54

1 Answers1

1

This is probably the result of an exception inside the TimerElapsed method, that prevents the timer from restarting. To aviod this, here's what you do:

  1. Set the AutoReset property of your timer to false.

  2. Inside the TimerElapsed event handler, in the finally clause, start the timer again.

This means that even if an exception occured in your TimerElapsed event handler, the timer will start again.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • 1
    Since all is done there is setting a bool and invoking `WriteToEventViewer`, my bet is on the `WriteToEventViewer` method, which is called before the try block. So, OP may also want to fortify that method itself. – Fildor Jan 14 '20 at 10:05
  • System.Diagnostics.EventLog requires elevated permissions (see https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlogpermission?view=netframework-4.8), so it is probably throwing a permissions exception in WriteToEventViewer. – Polyfun Jan 14 '20 at 10:25