I'm having a problem with my delay method. The overhead of the delay function is causing my program to slow down way to much. I go from a 0ms delay doing 7000 iterations in 1.466 seconds to a 1 ms delay doing 7000 iterations in 32.318 seconds. The math doesn't add up. Is there any way to get a more precise delay without slowing it down too much?
It's a multi-threaded app updating a simple graph in real-time:
public static void DrawData(int[] data, int size)
{
myBuffer.Graphics.Clear(System.Drawing.Color.White);
int x = 0;
int y = 0;
for (int i = 0; i < size; i++)
{
x = (i + 2) * width;
y = 200 - data[i];
myBuffer.Graphics.DrawLine(bluePen, x, 200, x, y);
}
myBuffer.Render();
System.Threading.Thread.Sleep(delay);
}
Loading
VulpesPosted Mar 16, 2011, 5:42 AM
Curtis StubbsPosted Mar 15, 2011, 9:50 PM
int time = (int)Environment.TickCount;
System.Threading.Thread.Sleep(delay);
actualDelay = (int)Environment.TickCount - time; ;
It's interesting to note that when delay is 0 actualDelay is also 0, but when delay is 1 actualDelay is 15. Also when delay is 16, actualDelay is 30. I am thinking when I set delay to 0 the compilier is optimizing the delay out of the code. It seems to only be accurate up to 15 milliseconds.
Curtis StubbsPosted Mar 15, 2011, 9:35 PM
int time = (int)Environment.TickCount;
while (Environment.TickCount - time < delay) ;
But the results were the same.
Also with the Thread.Sleep method if it sleeps for 0 milliseconds it still releases the process. Since the program is running really fast with sleep(0) I don't think this the problem.
VulpesPosted Mar 15, 2011, 7:31 PM
VulpesPosted Mar 15, 2011, 6:40 PM