<p>Imagine you want to animate some object on a WinForm. You setup a timer to update the state or model, and override the paint event of the Form. But from there, what's the best way to continually repaint the Form for the animation?</p>

<ul>
<li>Invalidate the Form as soon as you are done drawing?</li>
<li>Setup a second timer and invalidate the form on a regular interval?</li>
<li>Perhaps there is a common pattern for this thing?</li>
<li>Are there any useful .NET classes to help out?</li>
</ul>

<p>Each time I need to do this I discover a new method with a new drawback. What are the experiences and recommendations from the SO community?</p>

[{'answer_id': 11329, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 2, 'selected': False, 'text': '<p>What you\'re doing is the only solution I\'ve ever used in WinForms (a timer with constant redrawings). There are a bunch of techniques that you can use to make the user\'s experience with it smoother (such as double-buffering).</p>\n\n<p>You might want to give WPF a try. There are built-in facilities for doing animations in WPF, and they\'re much smoother (and require less code and no synchronization on your part) than a timer-based solution.</p>\n\n<p>Note that you do not need to use WPF throughout your whole app for that solution; it\'s possible to pack this functionality into a WPF control and embed the control in a WinForms application (or an unmanaged app, for that matter):</p>\n\n<p><a href="http://www.codeproject.com/KB/WPF/WPF_UserControls.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/WPF/WPF_UserControls.aspx</a></p>\n'}, {'answer_id': 11404, 'author': 'Peteter', 'author_id': 1192, 'author_profile': 'https://Stackoverflow.com/users/1192', 'pm_score': 4, 'selected': True, 'text': '<p>In some situations, it\'s faster and more convenient to not draw using the paint event, but getting the Graphics object from the control/form and painting "on" that. This may give some troubles with opacity/anti aliasing/text etc, but could be worth the trouble in terms of not having to repaint the whole shabang. Something along the lines of:</p>\n\n<pre><code>private void AnimationTimer_Tick(object sender, EventArgs args)\n{\n    // First paint background, like Clear(Control.Background), or by\n    // painting an image you have previously buffered that was the background.\n    animationControl.CreateGraphics().DrawImage(0, 0, animationImages[animationTick++])); \n}\n</code></pre>\n\n<p>I use this in some Controls myself, and have buffered images to "clear" the background with, when the object of interest moves or need to be removed.</p>\n'}, {'answer_id': 1155969, 'author': 'Richard Shepherd', 'author_id': 141066, 'author_profile': 'https://Stackoverflow.com/users/141066', 'pm_score': 6, 'selected': False, 'text': '<p>I\'ve created a library that might help with this. It\'s called Transitions, and can be found here: <a href="https://github.com/UweKeim/dot-net-transitions" rel="nofollow noreferrer">https://github.com/UweKeim/dot-net-transitions</a>.  Available on nuget as the <a href="https://www.nuget.org/packages/dot-net-transitions/" rel="nofollow noreferrer">dot-net-transitions package</a></p>\n\n<p>It uses timers running on a background thread to animate the objects. The library is open-source, so if it is any use to you, you can look at the code to see what it\'s doing.</p>\n'}]
['https://Stackoverflow.com/questions/11318', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/322/']