Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6949359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:56:38+00:00 2026-05-27T13:56:38+00:00

I have a Windows service written in C# which is meant to perform a

  • 0

I have a Windows service written in C# which is meant to perform a task every few minutes. I’m using a System.Timers.Timer for this but it doesn’t ever appear to fire. I’ve looked at many different posts here on SO and elsewhere and I’m not seeing what is wrong with my code.

Here is my code, with non-timer related items removed for clarity…

namespace NovaNotificationService
{
    public partial class NovaNotificationService : ServiceBase
    {
        private System.Timers.Timer IntervalTimer;
        public NovaNotificationService()
        {
            InitializeComponent();
            IntervalTimer = new System.Timers.Timer(60000);  // Default in case app.config is silent.
            IntervalTimer.Enabled = false;
            IntervalTimer.Elapsed += new ElapsedEventHandler(this.IntervalTimer_Elapsed);
        }

        protected override void OnStart(string[] args)
        {
            // Set up the timer...
            IntervalTimer.Enabled = false;
            IntervalTimer.Interval = Properties.Settings.Default.PollingFreqInSec * 1000;
            // Start the timer and wait for the next work to be released...
            IntervalTimer.Start();
        }

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

        private void IntervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {   // Do the thing that needs doing every few minutes...
            DoWork();
        }
    }
}

I’m really scratching my head over this one. Can anybody spot what silly thing I’m getting wrong?

EDIT:
By suggestion, I added IntervalTimer.Enabled = true; before IntervalTimer.Start(); in the service OnStart method. This doesn’t resolve the issue.

I’ve added file trace logging into the service to confirm some of the internals and I know for sure that the Timer.Enabled value is true by the time OnStart() is finished.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:56:39+00:00Added an answer on May 27, 2026 at 1:56 pm

    Here is my work-around…

    After way too many hours searching for an answer to this, I discovered a wide variety of articles and blogs discussing timers in Windows services. I’ve seen a lot of opinions on this and they all fall into three categories and in descending order of frequency:

    1. Don’t use System.Windows.Forms.Timer because it won’t work. (this only makes sense)

    2. Don’t use System.Threading.Timer because it doesn’t work, use System.Timers.Timer instead.

    3. Don’t use System.Timers.Timer because it doesn’t work, use System.Threading.Timer instead.

    Based on this, I tried 2. This is also the approach that seems to be recommended by Microsoft since they say that System.Timers.Timer is suited to “Server applications”.

    What I’ve found is that System.Timers.Timer just doesn’t work in my Windows Service application. Therefore I’ve switched over to System.Threading.Timer. It’s a nuisance since it requires some refactoring to make it work.

    This is approximately what my working code looks like now…

    namespace NovaNotificationService
    {
        public partial class NovaNotificationService : ServiceBase
        {
            private System.Threading.Timer IntervalTimer;
            public NovaNotificationService()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                TimeSpan tsInterval = new TimeSpan(0, 0, Properties.Settings.Default.PollingFreqInSec);
                IntervalTimer = new System.Threading.Timer(
                    new System.Threading.TimerCallback(IntervalTimer_Elapsed)
                    , null, tsInterval, tsInterval);
            }
    
            protected override void OnStop()
            {
                IntervalTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
                IntervalTimer.Dispose();
                IntervalTimer = null;
            }
    
            private void IntervalTimer_Elapsed(object state)
            {   // Do the thing that needs doing every few minutes...
                // (Omitted for simplicity is sentinel logic to prevent re-entering
                //  DoWork() if the previous "tick" has for some reason not completed.)
                DoWork();
            }
        }
    }
    

    I hate the “Doctor, doctor, it hurts when I do this…” solution, but that’s what I had to resort to. One more opinion on the pile for the next guy with this problem…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a windows service written in .net 4 which doing jobs periodically using
I have a windows service written in c#. It has a timer inside, which
I have written a windows service application which is installed on my PC. There
I have written a .NET Windows service which has a WCF service built into
I have a windows service executable that I know is written in .NET which
I have a very simple VB.net Windows Service written using VS.net 2008. The program
I have written a Windows service which should turn the display OFF when certain
I have a Windows Service written in .NET C# which calls a COM component.
I have a application written in C#, installed as a [WCF] windows service, which
I have a windows Service written in C# 4.0 which uses Microsoft Exchange WebServices(EWS)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.