I have been using C # for some time to make a small game, and while testing this game on another PC, I came across some strange elapsed time issues.
I have everything updated in this game, based on the time elapsed since the last game cycle, as in most cases, but everything was in order on the second PC.
I found out that the problem was creating a TimeSpan using the FromTicks()
method. I did a little test using the following code:
class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); sw.Stop(); TimeSpan t = TimeSpan.FromTicks(sw.ElapsedTicks); Console.WriteLine(t.ToString()); Console.WriteLine(sw.Elapsed.ToString()); Console.ReadKey(); } }
On my main PC, I ran this program and got the following:
00: 00: 00.3528353
00: 00: 00.9856987
Something I did not expect at all. I thought the second result was rather inaccurate, but the first was in excellent condition.
Then I ran the same program on another PC and got the following:
00: 03: 20.6866734
00: 00: 00.998287
I was very surprised.
My question here is not how I can fix this problem, I have already decided to use the second method, because it is accurate enough ... rather, I ask for enlightenment.
How could this be so? Why is the first result so inaccurate? Why does this change a lot on another machine?
I checked msdn in case I used the method incorrectly, but the examples there show that my results should be impossible ...
Note:
I think the CMOS battery is dying / dead, is that a factor?