Sleep() in Win32 makes program unresponsive

后端 未结 2 407
眼角桃花
眼角桃花 2021-01-24 07:04

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

相关标签:
2条回答
  • 2021-01-24 07:25

    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.

    0 讨论(0)
  • 2021-01-24 07:39

    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.
    0 讨论(0)
提交回复
热议问题