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

  • Home
  • SEARCH
  • 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 6543325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:19:58+00:00 2026-05-25T11:19:58+00:00

Possible Duplicate: System.Timer elapsed event seems to fire late for short intervals in .Net

  • 0

Possible Duplicate:
System.Timer elapsed event seems to fire late for short intervals in .Net

I use System.Timers.Timer in my windows service. Interval set to 60000 ms, but every next elapse, timer interval increases on some ms..Here example of log:

2011-09-05 00:00:37,177 [80] INFO - 
timer_Elapsed;

2011-09-05 00:01:37,187 [8] INFO  - 
timer_Elapsed;

2011-09-05 01:24:38,279 [71] INFO   - 
timer_Elapsed;

But I need to elapse timer in 37 seconds, as example, but after work some time, we have elapse timer in 38, 39, 40 .. sec..

I don’t need very precision timer, but i need to elapse 37 +- 1sec every time..

How i can resolve this problem???


I one time set Interval property in:

protected override void OnStart(string[] args)
{
   log4net.Config.XmlConfigurator.Configure();
   EmailReminderService.Logger.Info("Service started");

   _timer.Interval =Convert.ToInt32(ConfigurationManager.AppSettings["Interval"]);
   _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
   _timer.Enabled = true;               
}

private void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
   EmailReminderService.Logger.Info("timer_Elapsed;");
   EmailReminderManager manager = new EmailReminderManager();
   manager.EmailReminderProcessing();
}

In timer elapsed method not use _timer object..

  • 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-25T11:19:59+00:00Added an answer on May 25, 2026 at 11:19 am

    On each iteration calculate how the time difference between now and the point in time where you want to trigger the next timer. Then set your interval to that value. This will prevent a drift.

    //Once
    DateTime start=DateTime.UtcNow;// Or if you prefer that the event
                                   // happens on the same second no matter when
                                   // the program started you can simply
                                   // use a constant here
    
    //At the end of the timer handler
    int interval=(int)((DateTime.UtcNow-start).TotalSeconds*1000);
    const int targetInterval=60000;
    interval(interval+targetInterval/2)%targetInterval+targetInterval/2;
    timer.Interval=interval;
    

    This chooses an interval between 30 and 90 seconds so that you will hit the target time exactly.

    The program deals gracefully with UtcNow<start since that may happen when the user changes the system clock.

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

Sidebar

Related Questions

Possible Duplicate: Why should I use templating system in PHP? I was just curious
Possible Duplicate: Use a System.Drawing.Printing.PrintDocument to generate a PDF in memory I wrote a
Possible Duplicate: .NET file system wrapper library I would like to write a test
Possible Duplicate: Timer, event and garbage collection : am I missing something? private void
Possible Duplicate: How to update GUI from another thread in C#? My Timer event
Possible Duplicate: Choosing between MEF and MAF (System.AddIn) Is the Managed Extensibility Framework a
Possible Duplicate: javascript: pause setTimeout(); Im using jQuery and working on a notification system
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: System.Random keeps on returning the same value I'm refactoring and expanding a

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.