boolean f=0;
timer1.Interval=1;
private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
if (f == 0)
{
if (pictureBox1.Left < 200)
{
pictureBox1.Left += 1;
}
else
{
f = 1;
}
}
else
{
if (pictureBox1.Left > 100)
{
pictureBox1.Left -= 1;
}
else
{
f = 0;
}
}
}
}
this code move picture box on width form.
but speed of move this picture very very slow.
how can move picture by more speed?
edit
public void a()
{
while (true)
{
if (f == 0)
{
while (pictureBox1.Left < 200)
{
pictureBox1.Left += 1;
Thread.Sleep(1);
}
f = 1;
}
else
{
while (pictureBox1.Left > 100)
{
pictureBox1.Left -= 1;
Thread.Sleep(1);
}
f = 0;
}
}
}
ts=new ThreadStart(a);
t=new Thread(ts);
t.Start();
can use thread for this work but i want use timer
You cannot get this. Timers in Windows operate no faster than the clock interrupt rate, 1/64 second by default. The earliest the Tick event can run, assuming no other delays occur in your UI thread, is 15.625 msec. Since you move the box by one pixel for 100 pixels, this will take 100 x 15.625 = 1.56 seconds. Yes, that’s slow.
Still, that’s 64 updates per second, that’s not slow. It is overkill, the human eye can’t keep up with that. A movie in the cinema updates at 24 frames per second. The simple problem is that your positioning increment is too small.
A good timer Interval value is 45 msec, gets you 21 updates per second. Adjust the position increment by how much you want to move per update. You still might not get that if painting the Image plus updating the container background takes longer. The Image can be expensive to draw if it has to be resized to fit the picture box or when its pixel format is not 32bppPArgb.