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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:29:28+00:00 2026-05-23T17:29:28+00:00

I have 2 computers whose time is synchronized through NTP which assures that the

  • 0

I have 2 computers whose time is synchronized through NTP which assures that the time will differ only by a few milliseconds. One of the computers will send a message through TCP to the other one to start a certain c# function at a specified time in the future on both computers.

My question is: How can i trigger a function in C# at a certain time with millisecond precision (or better)? I need to do that in the program code ( so Task Scheduler or other external program will not help). Always cycling in a separate thread to compare current time with target time would not be a good solution i guess.

UPDATE:

DateTime.Now can’t be used in the solution since it has low resolution.

It seems Thread.Sleep() can be forced to have a 1 ms resolution by importing:

[DllImport("winmm.dll", EntryPoint="timeBeginPeriod")]
public static extern uint MM_BeginPeriod(uint uMilliseconds);

and using:

MM_BeginPeriod(1);

To revert to the previous resolution import:

[DllImport("winmm.dll", EntryPoint = "timeEndPeriod")]
public static extern uint MM_EndPeriod(uint uMilliseconds);

and use:

MM_EndPeriod(1);

UPDATE 2:

I tested Thread.Sleep() with many values and it seems that as an average it will tend towards the specified time-span.

Calling Thread.Sleep() only once usually stays around half a ms around the target value time-span so it is pretty precise regarding to a millisecond resolution.

Using the winmm.dll methods timeBeginPeriod and timeEndPeriod seems to have no effect on the accuracy of the result.

SOLUTION:

One method would be to use timeSetEvent(deprecated) or CreateTimerQueueTimer.

Current problem is that both need as a parameter the time left untill function triggers instead of the time at which it should trigger.
So the delay until desired time for the trigger must be calculated but DateTime.Now offers low resolution. I found a class that allows high resolution obtaining of current DateTime.
So now the time left can be calculated with high resolution and be passed as a parameter to CreateTimerQueueTimer.

  • 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-23T17:29:29+00:00Added an answer on May 23, 2026 at 5:29 pm

    This should give you one event every millisecond. You can use a stopwatch to measure the elapsed time. Trigger an event on the main UI thread by using invoke so you don’t block the timer.

        public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2);
    
        /// <summary>
        /// A multi media timer with millisecond precision
        /// </summary>
        /// <param name="msDelay">One event every msDelay milliseconds</param>
        /// <param name="msResolution">Timer precision indication (lower value is more precise but resource unfriendly)</param>
        /// <param name="handler">delegate to start</param>
        /// <param name="userCtx">callBack data </param>
        /// <param name="eventType">one event or multiple events</param>
        /// <remarks>Dont forget to call timeKillEvent!</remarks>
        /// <returns>0 on failure or any other value as a timer id to use for timeKillEvent</returns>
        [DllImport("winmm.dll", SetLastError = true,EntryPoint="timeSetEvent")]
        static extern UInt32 timeSetEvent(UInt32 msDelay, UInt32 msResolution, TimerEventHandler handler, ref UInt32 userCtx, UInt32 eventType);
    
        /// <summary>
        /// The multi media timer stop function
        /// </summary>
        /// <param name="uTimerID">timer id from timeSetEvent</param>
        /// <remarks>This function stops the timer</remarks>
        [DllImport("winmm.dll", SetLastError = true)]
        static extern void timeKillEvent(  UInt32 uTimerID );
    
        TimerEventHandler tim = new TimerEventHandler(this.Link);
        public void Link(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2)
        {
            _counter++;
            if( (_counter % 10 ) == 0)
                setLblTxt();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have roughly 12 computers that each have the same script on them. This
I have some expensive function f(x) that I want to only calculate once, but
I have two computers. Both running WinXP SP2 (I don't really know ho similar
I have three computers on my LAN, one running ubuntu , one running openSuse
Why do computers have byte-addressable memory, and not 4-byte-addressable memory (or 8-byte-addressable memory for
I have 2 computers. Both with w7 as os. I have installed wampserver on
Say, I have two computers behind firewalls, routers, etc (ie. no incoming connections). Is
I have seen new computers having two processors on same motherboard . How can
At my house I have about 10 computers all different processors and speeds (all
I am 22 Years of age and have been into computers since 13 and

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.