When I run this code, the text is updated AFTER the message box in the thread is popped.
void PnlOptions::ClickHandler() {
SetWindowText(txt_progress_, "CLASS MEMBER FUNCTION");
HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &ThreadProcess, 0, CREATE_SUSPENDED, 0);
ResumeThread(hThread);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
}
unsigned int __stdcall ThreadProcess(void * data0) {
MessageBox(NULL, "THREAD FREE FUNCTION", "Alert", MB_OK);
}
I thought it was because
If the thread is created in a runnable state (that is, if the CREATE_SUSPENDED flag is not used), the thread can start running before CreateThread returns and, in particular, before the caller receives the handle and identifier of the created thread.
but using a non-suspended thread: same result.
Also tried:
-
Using
CreateThread -
Changing thread priority
-
Using
SendMessageinstead ofSetWindowText -
PeekMessage
Why does the thread start before the UI is updated?
Declarations:
pnl_options.h:
unsigned int __stdcall ThreadProcess(void *);
public PnlOptions:
void Init(HWND);
void ClickHandler();
private:
HWND txt_progress_;
pnl_options.cpp (other than above code):
void PnlOptions::Init(HWND hwnd0) {
txt_progress_ = CreateWindowEx (0,
TEXT("EDIT"), "Press \"GO\" to process all selected files.",
SS_LEFT | SS_CENTERIMAGE | WS_VISIBLE | WS_CHILD,
0, 0, 0, 0,
hwnd0, (HMENU) IDT_PROGRESSTEXT, NULL, NULL
);
}
I reproduced this behavior and it seems that there is a kind of deadlock between the EDIT control and his parent window because of a posted WM_PAINT message.
“Curioser”, it works if you replace the EDIT with a STATIC control.
I don’t have real explanation/solution for this, so it’s more a clue than an answer…
PS: Note that SS_LEFT and SS_CENTERIMAGE are not valid for an EDIT control, use ES_* defines instead.