
| Last Modification: December 11, 1999 |
Why do I get an assertion in MFC when my thread calls a function in the main
thread?
The MFC message processing code is not thread-safe. So when a secondary thread calls anything in the main thread that affects a CWnd-derived object there is a danger of data corruption within MFC. To guard against this MFC uses thread local storage and liberal ASSERTs to warn you that what you are trying to do can't work reliably. There are two safe alternatives for updating a main thread window from a secondary thread.
// force repaint of a window in the main thread
::InvalidateRect(hwnd, NULL, TRUE);
// change the text in any control
::SetDlgItemText(hwnd, IDC_CONTROL, "New text");
References and Samples: