Please see my past answers:
Drawing Lines between mdi child forms[
^],
capture the drawing on a panel[
^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^].
The point is: you always paint in
Paint
event or in overridden
OnPaint
, and in timer handle you only call
Invalidate
(if you use timers at all, I would rather recommend to use a thread instead, with the same rendering technique). Only this way, you get all control over rendering.
Also, never use
System.Windows.Forms.Timer
. It's only good when you have no requirements on the accuracy at all, for example, if you don't care if you wait twice or more as long. Yes, this is that bad.
And it can be much better to use a thread with
Sleep
instead of timer. If you want time, you can poll the value of
System.DateTime.Now
or
Stopwatch
every time you are about invalidation of the rendered scene. In can give you much better accuracy then a timer, but with timer, you can get additional hassles. Did you ever tried to resolve the situations when a timer event happens when the handler invoked by the previous timer tick is not yet completed? Timers are hard to deal with.
With other timers, the problem is that you the event is invoked in some other thread. So, anyway, you would need to use
invocation mechanism. 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