Hi Slip I have recently worked on a solution that is similar to your needs. I used threads that animated a user control containing other controls, in fact I had multiple user controls being animated on the same form, with different threads. I used delegates to access a number of variables on the form from my threads. Also in the form I declared a volatile integer like this:- private volatile int componentOnPaint
And a public method which will be used by a delegate to return the above value. Then in your override method for OnPaint in the form, increment the componentOnPaint integer, this way you know how many times the form has been re-painted. Before your thread has invalidated the form get the componentOnPaint value, invalidate the form and then sleep for a short time using Thread.Sleep(30) (this allows the thread to be re-painted). At the next line of execution check the componentOnPaint value again, if it is greater than before, then continue. Otherwise loop and sleep again, until the value is greater and the form is repainted. Then you would use Thread.Sleep again for a fixed time depending on the animations frame length. To make the animation smoother, use another delegate to get the delay time (milliseconds) required in between animation cycles. Use DateTime.Now to get the time before you invalidated your form, and use DateTime.Now to get the time after componentOnPaint was greater. Subtract the first value from last eg... TimeSpan drawTime = AnimateLastStart - AnimateLastDrawn;
Then reduce your delay time by the time it took to refresh your form... ie... Thread.Sleep(standardTime-drawTime.Milliseconds)
This worked for me, using volatile earlier means the variable can act as a semaphore accessible from any thread (hence volatile), so you know if the form has been drawn and you shouldn't miss any frames. The time manipulation should just make your animation smoother. I hope this helps :-) Jason -- modified at 7:04 Wednesday 4th July, 2007
J
Jason Morley
@Jason Morley
Posts
-
Animation timing