0

I am having a problem get code to run through a system timer on a service , well actually no the timer IS firing , however the code below it will not run.

    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("Service Started");
        //ad 1: handle Elapsed event 
    timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

    //ad 2: set interval to 1 minute (= 60,000 milliseconds)

    timer.Interval = 60000;
    timer.AutoReset = true;
    //ad 3: enabling the timer
    timer.Enabled = true;
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
    }




    private void OnElapsedTime(object source, ElapsedEventArgs e)
         {
            EventLog.WriteEntry("TIMER FIRED");
            string[] filepaths = Directory.GetFiles(path, Filetype);
            string result;

            // Process Each String/File In Directory
            foreach (string s in filepaths)
            {
                for (int i = 0; i < filepaths.Length; i++)
                {
                    //Result Returns Video Name
                    result = Path.GetFileNameWithoutExtension(filepaths[i]);

                    PreformTranslation(filepaths[i], outputPath + result);

                    if (File.Exists(path + result))
                    {
                        MoveVideoFiles(path + result, outputPath + result);
                    }
                }
            }
         }

So i See from the event viewer that it actually sends the "timer Fired" off each minute , but does not precede to run any of the code below. Without the timer in place the code runs as it should, so inititally i just tried calling the method, when that didn't work i just placed the code in the event handler hoping that would change. I think I've already lost when i resort to hope.

user685590
  • 2,363
  • 4
  • 28
  • 42
  • What exception is thrown? Do you have access to the folder specified in path? – agent-j Jul 14 '11 at 15:19
  • 2
    How are you determining that the code after writing the event log entry isn't running? – E.Z. Hart Jul 14 '11 at 15:21
  • 2
    It's extremely unlikely to the point of impossible that the code *below* your sentinel `EventLog.WriteEntry` call is *not* getting executed, while the sentinel code *is* being executed. More likely, you're just not seeing any *results* from the operation. That's likely because your algorithm is wrong, an exception is being thrown, or something similar. – Cody Gray Jul 14 '11 at 15:30
  • Dammit!. Yes stuck in a try catch and found a IO exception , with all my muddling/headbangging around i forgot to call getINI() which reads the directory paths in !. So annoyed at myself!. Seems to be working perfectly now. Now to test it on the server... – user685590 Jul 14 '11 at 15:51
  • 1
    And now you've learned the hard way why catching `System.Exception` is a **bad, bad, bad idea**. Only catch the specific exceptions that you know you can handle, and log them anyway. Otherwise, just let the exceptions bubble up the stack. More detail here: http://stackoverflow.com/questions/6505120/global-handling-exception-in-window-service/6505237#6505237 – Cody Gray Jul 14 '11 at 16:06

0 Answers0