Take the 2-minute tour ×

Basically exactly what the title says. I would like to update the text that a button contains every 1 second when the user presses that particular button. I have noted that when the program doesn't have focus it works alright and the text refreshes correctly but when I am hovering over the program or when I am trying to click on it's menu Windows inform me that the program is unresponsive and asks me if I want it terminated. When the loop finishes the program returns to its normal state. Also any action I might have done (like moving it around or closing it) while it was Sleep()-ing is executed after the loop. Here is a bit of code:

// Code executed when pressing Start Button.
char startButtonText[30];  // Storing next loop text
       sprintf(startButtonText, "Starting in ... %d", i);
       SendMessage(hwndButtonStart, WM_SETTEXT, 0, (LPARAM)(startButtonText));

Is this normal? If not what's causing this?

share|improve this question
Sleep after SendMessage? Did you intend PostMessage instead by any chance? –  Chubsdad Dec 13 '12 at 10:18
Sleep() is the solution to practically no problems –  David Heffernan Dec 13 '12 at 11:20
add comment

2 Answers

up vote 8 down vote accepted

The WndProc does not process messages asynchronously within an application which means all messages are expected to be handled quickly and a return value delivered immediately. You must not Sleep in the UI thread since it will block other UI events from being processed. Any heavy work or synchronous requests/jobs which are likely to take a long time should be performed in worker threads. There are at least three viable options:

  1. Create a new (worker thread) for the task.
  2. If the task is likely to be done often, use a thread pool instead.
  3. Set and subscribe to timer events.
share|improve this answer
Your first 2 sentences cleared this out for me. Thank you very much. +1 and accepted. –  Theocharis K. Dec 13 '12 at 10:20
add comment

I think the call to Sleep() might be keeping you from returning from the WndProc, so your application is not processing the incomming events for 5 secs. I suggest you try to subscribe to 5 timer events in 1s, 2s,..., 5s. Like when the timer message is recieved the button text must change. I don't know a way how to do that off the top of my head.

share|improve this answer
add comment

Your Answer


