简体   繁体   中英

How to measure time in millisecond precision by thread

I need to measure an event time precisely (suppose the time between two clicks). I used a timer and set the interval to "1" and a counter inside Tick event. The problem is since there are a lot of lines in my code the Tick event does not actually happen every 1ms (maybe every 8ms or more). Is there any way to use a thread and run a 1ms-timer independently in it?

        //start
        long start = DateTime.Now.Ticks;

        //to do something
        //...

        //end
        long end = DateTime.Now.Ticks;
        long delta = end - start;
var stopWatch = Stopwatch.StartNew();

// do something

stopWatch.Stop();

Console.WriteLine(stopWatch.ElapsedMilliseconds);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM