Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

System.Timers.Timer Elapsed event not firing in Windows Service

c# windows service timers

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 Tchpowdog

Tchpowdog

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 21 May 2015 - 06:37 AM

I recently purchased a very weak VPS from GoDaddy to test a website I have developed.  I've created a simple service that I would like to trigger every 10 seconds to send pending emails to users from my database.  I've created many services before that used a System.Timers.Timer and I've never had a problem with it.  This is the first service I've installed on the VPS, however.  The timer is simply not firing.  I know this because I keep my own EventLog in a txt file and the Service's OnStart() works and writes to my EventLog.  After that, nothing...  I have tried everything under the sun to get this to work and I just can't.  The mysterious thing is that even a System.Threading.Timer's callback DOES NOT fire.  Could this be an issue with the VPS??  Please don't tell me I'm going to have to put this in a Scheduled Task!! lol
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using FieldEncryption;
using Microsoft.Exchange.WebServices.Data;
using System.Net;
using System.Threading;
 
namespace EmailsPendingSvc
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer = new System.Timers.Timer();
 
        public Service1()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
            timer.Interval = 10000;
            timer.Elapsed += timer_Elapsed;
            timer.Start();
            using (StreamWriter sw = new StreamWriter(@"C:\Program Files\PHServices\EmailsPendingSvc\EventLog.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToShortDateString() + "  --  " + DateTime.Now.ToShortTimeString() + "  --  Service Started.");
            }
        }
 
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(@"C:\Program Files\PHServices\EmailsPendingSvc\EventLog.txt", true))
            {
                sw.WriteLine("Timer elapsed.");
            }
            timer.Stop();
            //   Do stuff
            timer.Start();
        }
 
        protected override void OnStop()
        {
            using (StreamWriter sw = new StreamWriter(@"C:\Program Files\PHServices\EmailsPendingSvc\EventLog.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToShortDateString() + "  --  " + DateTime.Now.ToShortTimeString() + "  --  Service Stopped.");
            }
        }
 
        static bool RedirectionCallback(string url)
        {
            return url.ToLower().StartsWith("https://");
        }
    }
}





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download