سؤال

I am writing some code to detect HID idle times.

var before = DateTime.Now.Ticks;
Console.WriteLine(before);
System.Threading.Thread.Sleep(5000);
var after = DateTime.Now.Ticks;
Console.WriteLine(TimeSpan.FromTicks(after - before).Milliseconds);

I should get 5 milliseconds in the output, but it's not. It's pretty random. What did I do wrong?

Note to self: I shall try system.diagnostic.stopwatch now

هل كانت مفيدة؟

المحلول

Change to

Console.WriteLine(TimeSpan.FromTicks(after - before).TotalMilliseconds);

This will give you the result you want.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top