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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:12:49+00:00 2026-06-02T19:12:49+00:00

In my previous question how to suspend a thread for 1 ms? it was

  • 0

In my previous question how to suspend a thread for 1 ms? it was answered that to introduce delay of 1 ms I should use Timer.

I need to “do work” then wait 2 ms in average (1-5 ms is ok, it’s ok to have rarely extreme delays up to several hundreads ms), then do work again. It’s important not to try to start new iteration when previous iteration still not finished, so I don’t need to do something every 1-5 ms, I need 1-5 ms delay between iterations.

I’ve tried to do that using System.Timers.Timer but it doesn’t work:

using System;
using System.Diagnostics;
using System.Threading;
using System.Timers;
using Timer = System.Timers.Timer;

namespace TestTimer
{
    class Program
    {
        static Timer refresh_timer;
        private static int called;
        private const int ITERATIONS = 100;
        private static Stopwatch sw;

        static void Main(string[] args)
        {
            refresh_timer = new Timer(1);
            refresh_timer.AutoReset = false;
            refresh_timer.Elapsed += OnRefreshTimedEvent;
            sw = Stopwatch.StartNew();
            refresh_timer.Start();
            Thread.Sleep(10000);
        }

        static void OnRefreshTimedEvent(object source, ElapsedEventArgs args)
        {
            DoGateIteration();
        }

        private static void DoGateIteration()
        {
            //try
            //{
                // work here
                called++;
            //}
            //finally
            //{
                if (called == ITERATIONS)
                {
                    Console.WriteLine("Average iterations per second: " + ITERATIONS * 1000 / sw.ElapsedMilliseconds);
                    Console.WriteLine("Average iterations milliseconds: " + sw.ElapsedMilliseconds / ITERATIONS);
                }
                refresh_timer.Start();
            //}
        }
    }
}

It reports:

Average iterations per second: 64
Average iterations milliseconds: 15

So it seems System.Timers.Timer is not as precise as I need.
Probably I should try System.Threading.Timer, but this timer doesn’t have AutoReset property that I need.

What would you suggest?

  • 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-06-02T19:12:50+00:00Added an answer on June 2, 2026 at 7:12 pm

    For such a short delay of only 1 or 2 milliseconds, it’s probably best just to busy-wait:

    private static int called;
    private const int ITERATIONS = 10000;
    private static Stopwatch sw;
    
    static void Main(string[] args)
    {
        sw = Stopwatch.StartNew();
    
        var x = Stopwatch.StartNew();
        while (true)
        {
            DoGateIteration();
    
            x.Restart();
            while (x.ElapsedMilliseconds < 2) ; // busy-wait for 2 ms
        }
    }
    
    private static void DoGateIteration()
    {
        called++;
        if (called == ITERATIONS)
        {
            Console.WriteLine("Average iterations per second: " + ITERATIONS * 1000 / sw.ElapsedMilliseconds);
            Console.WriteLine("Average iterations milliseconds: " + sw.ElapsedMilliseconds / ITERATIONS);
        }
    }
    

    Output:

    Average iterations per second: 500
    Average iterations milliseconds: 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My previous question concluded that a distasteful double cast might be necessary to use
In previous question of mine, someone had meantioned that using Semaphores were expensive in
this question is an extension to a previous question i asked (and was answered).
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
On Previous Question , someone recommended that I install Andy's IDE Fix Pack to
My previous question about this subject was answered and I got some tests working
In previous question I got comment about Oracle statistics: Oracle doesn't know that 50M
This question follows on from a previous question which can be found here: Need
my previous question with source code is here: How to use gif animated image

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.