[WPF]Why DispatcherTimer doesn't give equal speed?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,

I am having a UserControl that can randomly move around, and I am using
DispatcherTimer, by setting its interval to TimeSpan.FromMilliseconds(10)

My code to execute the movement is as follows:

Random rand = new Random();
bool restart = false;
int movementX, movementY = 1;
void timer_Tick(object sender, EventArgs e)
{
if (restart)
{
movementX = rand.Next(0, 3);
movementY = rand.Next(0, 3);

movementX = (currentX > halfWidth) ? -movementX : movementX;
movementY = (currentY > halfHeight) ? -movementY :
movementY;

if (movementX == 0 && movementY == 0)
{
movementX = (currentX > halfWidth) ? -1 : 1;
movementY = (currentY > halfHeight) ? -1 : 1;
}
restart = false;
}

currentX += movementX;
currentY += movementY;
Translate.X += movementX;
Translate.Y += movementY;

if (currentX + this.Width >= parentWidth || currentY +
this.Height >= parentHeight || currentX <= 0 || currentY <= 0)
{
restart = true;
}
}


One thing I found is, when I have multiple this control, they are not moving
in an equal speed. Some are faster than others, then suddenly they might
become slower.

It's quite strange. Can anyone explain?

I can send you the source code so you can see what exactly I mean.

thanks in advance.





--
Best Regards!
Hong.
---------------------------
Image Processing and Watermark components for .NET
http://www.ImageComponent.NET

Personal Entertainment Shareware and Freeware
http://www.angGoGo.com


.