Hey, as of lately, I've been trying to find good ways to smoothen out thread sleeps (incase it gets disturbed or if your computer laggs etc).
So which is an overall "better performance" method?
JAVA's System.currentTimeMillis() method for C#:
public static double GetCurrentMilliseconds()
{
DateTime staticDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan timeSpan = DateTime.UtcNow - staticDate;
return timeSpan.TotalMilliseconds;
}
System.currentTimeMillis() in use:
public void Run()
{
this.lastUpdate = TimeUtilities.GetCurrentMilliseconds();
while (isRunning)
{
if (TimeUtilities.GetCurrentMilliseconds() - lastUpdate >= 600)
{
lastUpdate = TimeUtilities.GetCurrentMilliseconds();
// bla bla bla...
}
try
{
Thread.Sleep(100);
}
catch (Exception ex)
{
Jolt.GetLog().WriteException(ex);
}
}
}
and C#'s Environment.TickCount:
private void Run()
{
double lastUpdate = Environment.TickCount;
while (isRunning)
{
if (lastUpdate + 600 < Environment.TickCount)
{
lastUpdate = Environment.TickCount;
// bla bla bla...
}
try
{
Thread.Sleep(100);
}
catch (Exception ex)
{
Jolt.GetLog().WriteException(ex);
}
}
}
Any help would be appreciated. Otherwise if this is a bad idea, could you please provide a better way to do this.