EDN Admin
Well-known member
Hi.<br/><br/>I have a piece of code which must maintain a steady execution time between each loop. Ive been using timespan to calculate the processing time for the block before sleeping the thread, however Ive been unable to maintain the fixed rate because a timespan subtraction always results in total milliseconds values that are multiple of 15.6254. I cannot get any value lower than or in between 15.6254 and 31.2508, and so on.
<br/>
Is there any reason for this granularity, any workaround or any other way to measure the elapsed milliseconds in a precision higher than 15.6254?<br/><br/>Here goes the code block to represent what Im doing:
<pre lang="x-c# DateTime startTime = DateTime.Now;
int curWait = 79;
while (true)
{
/*
* Do some heavy processing here, mostly mathematical
* operations and read/write arrays and other variables.
*/
//This value is always a multiple of 15.6254.
int elapsed = (DateTime.Now - startTime).Milliseconds;
if (elapsed <= curWait)
{
Thread.Sleep(curWait - elapsed);
}
startTime = DateTime.Now;
}[/code]
Thank you,
<br/>
Tulio
View the full article
<br/>
Is there any reason for this granularity, any workaround or any other way to measure the elapsed milliseconds in a precision higher than 15.6254?<br/><br/>Here goes the code block to represent what Im doing:
<pre lang="x-c# DateTime startTime = DateTime.Now;
int curWait = 79;
while (true)
{
/*
* Do some heavy processing here, mostly mathematical
* operations and read/write arrays and other variables.
*/
//This value is always a multiple of 15.6254.
int elapsed = (DateTime.Now - startTime).Milliseconds;
if (elapsed <= curWait)
{
Thread.Sleep(curWait - elapsed);
}
startTime = DateTime.Now;
}[/code]
Thank you,
<br/>
Tulio
View the full article