简体   繁体   English

如何通过线程以毫秒为单位测量时间

[英]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. 我使用了一个计时器,并将间隔设置为“ 1”,并在Tick事件中设置了一个计数器。 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). 问题是,由于我的代码中有很多行,所以Tick事件实际上并不是每1毫秒(也许每8毫秒或更长时间)发生一次。 Is there any way to use a thread and run a 1ms-timer independently in it? 有什么方法可以使用线程并在其中独立运行1ms计时器吗?

        //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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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