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 761543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:51:02+00:00 2026-05-14T15:51:02+00:00

I have a Windows NT Service in C# which basically wakes up every x

  • 0

I have a Windows NT Service in C# which basically wakes up every x seconds, checks to see if any mail notifications need to be sent out, and then goes back to sleep.

It looks something like this (the Timer class is from the System.Threading namespace):

public partial class MyService : ServiceBase
{
    private Timer _timer;
    private int _timeIntervalBetweenRuns = 10000;

    public MyService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        // when NT Service starts - create timer to wake up every 10 seconds
        _timer = new Timer(OnTimer, null, _timeIntervalBetweenRuns, Timeout.Infinite);
    }

    protected override void OnStop()
    {
        // on stop - stop timer by freeing it
        _timer = null;
    }

    private void OnTimer(object state)
    {
        // when the timer fires, e.g. when 10 seconds are over 
        // stop the timer from firing again by freeing it
        _timer = null;

        // check for mail and sent out notifications, if required - works just fine
        MailHandler handler = new MailHandler();
        handler.CheckAndSendMail();

        // once done, re-enable the timer by creating it from scratch
        _timer = new Timer(OnTimer, null, _timeIntervalBetweenRuns, _timeIntervalBetweenRuns);
    }
}

Sending the mail and all works just fine, and the service also wakes up every 10 seconds (in reality, this is a setting from a config file – simplified for this example).

However, at times, the service seems to wake up too quickly….

2010-04-09 22:50:16.390
2010-04-09 22:50:26.460
2010-04-09 22:50:36.483

2010-04-09 22:50:46.500
2010-04-09 22:50:46.537  ** why again after just 37 milliseconds...... ??

2010-04-09 22:50:56.507

Works fine to 22:50:45.500 – why does it log another entry just 37 milliseconds later??

Here, it seems it’s totally out of whack…. seems to wake up twice or even three times every time 10 seconds are over….

2010-04-09 22:51:16.527

2010-04-09 22:51:26.537
2010-04-09 22:51:26.537

2010-04-09 22:51:36.543
2010-04-09 22:51:36.543

2010-04-09 22:51:46.553
2010-04-09 22:51:46.553

2010-04-09 22:51:56.577
2010-04-09 22:51:56.577

2010-04-09 22:52:06.590
2010-04-09 22:52:06.590

2010-04-09 22:52:06.600
2010-04-09 22:52:06.600

Any ideas why?? It’s not a huge problem, but I’m concerned it might start to put too much load on the server, if the interval I configure (10 seconds, 30 seconds – whatever) seems to be ignored more and more, the longer the service runs.

Have I missed something very fundamental in my service code?? Am I ending up with multiple timers, or something?? I can’t seem to really figure it out….. have I picked the wrong timer (System.Threading.Timer) ? There’s at least 3 Timer classes in .NET – why?? 🙂

  • 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-14T15:51:02+00:00Added an answer on May 14, 2026 at 3:51 pm

    Actually, the issue seems to be I picked the wrong timer class.

    With the System.Threading.Timer, I have those issues I explained in my original question.

    When I switched to System.Timers.Timer, all those issues went away.

    So this still begs the question: why on earth are there at least three different Timer classes in the .NET framework, and what are the (subtle) differences between them?

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

Sidebar

Related Questions

No related questions found

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.