Don't even play with the idea of intervention on the UI cycle (it is possible, dirty and ineffective, just a bad idea).
In all cases, use a separate thread with delay (
System.Threading.Thread.Sleep
). In this thread, change the picture periodically. Use one of these three method of obtaining a thread:
http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[
^].
If you want to create a thread each time you want to start this "animation", you should use
BackgroundWorker
(referenced above). You can also create a thread from the very beginning and throttle it with a event wait handle, set it when you want to start animation, reset to pause/stop it. Please see
System.Threading.EventWaitHandle
or
System.Threading.ManualResetEvent
:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[
^].
For the thread created by a thread constructor (see the very first reference above), it's the best to use a thread wrapper I offered in my past answers:
How to pass ref parameter to the thread[
^],
change paramters of thread (producer) after it started[
^].
Now, the problem is that you need to show the change in image in UI. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in 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[
^].
—SA