Today at work, I was working on a small application with my colleague. We had dialog box with a button, an edit control and a static control. What we wanted to do was show the status of the program on the static control once the button was pressed. And the code looked something like this:
case WM_COMMAND:
{
wmID = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch(wmID)
{
case IDM_BUTTON_GMC:
{
SetWindowText(GetDlgItem(hDlg, IDC_STATIC_STATUS), L"In progress..");
/*
Do some work
*/
SetWindowText(GetDlgItem(hDlg, IDC_STATIC_STATUS), L"Done.");
}
break;
}
.
.
.
}
break;
The default text on the static control was blank. When I ran the program we noticed that the static control never displayed "In Progress..". After finishing the actual meat of the job, the static control displayed "Done". We wondered whether it was because the actual work was happening too fast, and we put a sleep of 1 second after the first SetWindowText() call, but it didn’t solve the problem. We sat wondering about why this was happening. We checked the return value of the first SetWindowText call and it was non-zero which meant the call was successful. Another colleague of ours walked by and we explained the problem to her briefly, and she stared for a moment and said, UpdateWindow. And we all went "Aww…". So we put UpdateWindow(GetDlgItem(hDlg, IDC_STATIC_STATUS)); after both the calls and it worked, as expected. Painting is one of the most costliest operation and runs at a very low priority. So no wonder the calls before weren’t updating the text immediately. Sometimes we are all so occupied with bigger things that we miss out on the small ones.