0

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?

Dcember
  • 9
  • 2
  • I doubt you'll get that level of accuracy. With 8ms you should be glad. – rene Aug 05 '16 at 08:04
  • relevant: http://stackoverflow.com/a/28648/578411 but there is more to find when you search for `high precision timer [c#]` – rene Aug 05 '16 at 08:07
  • 2
    Typically if you need to *measure* an interval you would be using a `Stopwatch` not a `Timer`. – Mike Zboray Aug 05 '16 at 08:07
  • Well, what exactly are you trying to measure? If all your doing is counting in milliseconds then you probably don't want to use an actual count at all. Just get a timestamp difference when you need to use the value – musefan Aug 05 '16 at 08:07
  • @musefan it is actually the human reaction time in milliseconds. – Dcember Aug 05 '16 at 08:15
  • 1
    @Dcember: On first click, record timestamp. On second click, get timestamp and subtract the first from it. Then you have the answer – musefan Aug 05 '16 at 08:19

2 Answers2

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

        //to do something
        //...

        //end
        long end = DateTime.Now.Ticks;
        long delta = end - start;
Narek Arzumanyan
  • 616
  • 3
  • 11
1
var stopWatch = Stopwatch.StartNew();

// do something

stopWatch.Stop();

Console.WriteLine(stopWatch.ElapsedMilliseconds);
Mark
  • 101
  • 1
  • 7