Certainly, you need to use the thread. For a simple example, please see my past answer:
Changes step by step to show a control[
^].
Only instead of label, use your
PictureBox
object. For a random move, use the class
System.Random
:
http://msdn.microsoft.com/en-us/library/system.random.aspx[
^].
But don't just jump to random coordinate. Instead, jump to a small random vector relative to a current location of you moving object, just a few pixels. Generate a random value for
Δy
and
Δx
in the range, say, -5.. +5 pixels and move by this value:
myPictureBox.Left += deltaX;
myPictureBox.Top += deltaY;
To fight quite possible flicker, use
double buffering:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx[
^].
With the UI, the key here is using the UI invocation mechanism or
System.Threading.Dispatcher
. For more information, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
Good luck,
—SA