Writing on DC from the other thread is absolutely useless and even dangerous. All rendering should occur in the handler of a Windows message
WM_PAINT
. It always happens in your UI thread. So, the question is: how to make the rendered graphics changing with time and trigger this message? Well, not be sending or posting it. It's done via the Windows API
Invalidate
. In order to improve performance you can also call
InvalidateRect
or
InvalidateRect
with parameter (rectangle or region) to invalidate only the part of the scene.
See
http://msdn.microsoft.com/en-us/library/dd145005%28v=vs.85%29.aspx[
^].
Now, how to change graphics from a separate thread to create some animation effect? First of all, your thread makes no sense as it writes graphical data only once, but you could make the thread running in cycle with some delay, generating different text in each iteration. To do this, have a shared memory holding, say, the string you output. As this memory should be used by two threads (the string will be written by the thread you created and read by your UI thread), addressing to this memory should be inter-locked using critical section (this is the most light-weight method), via the methods
EnterCriticalSection/LeaveCriticalSection
, see:
http://en.wikipedia.org/wiki/Critical_section[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684169(v=vs.85).aspx[
^].
You additional (non-UI) thread should only modify this data and notify your UI thread about the change by invalidating the client area of the window.
—SA