C++ Windows Sleep() without stopping other tasks - c++

I know what I'm asking is contradictory
SendNumber(LastNumber);
Sleep(2000+(level*100));
SendNumber() is a function that sends LastNumber to an edit control with WM_SETTEXT. What I'm trying to do is sleep the program for a moment, leaving the text on the edit control, and after the time ends remove this text.
My problem is that Sleep() stops WM_SETTEXT from typing the text, so the program waits 2000+(level*100) milliseconds and then the text appears on the edit box.
Is there any way to stop the program from running forward until the time ends, but without stopping all the window activity?

Look at the SetTimer function. This will send your program a WM_TIMER message after a specified period, which you can then handle.

Add
UpdateWindow(your_edit_control);
just after SendNumber.

Related

C++ function Sleep() executes before piece of code

I work in Visual Studio with C++, Windows Form App. I try to paint the button red, wait 3 seconds and then paint it blue.
button1->BackColor = System::Drawing::Color::DarkRed;
Sleep(3000);
button1->BackColor = System::Drawing::Color::CornflowerBlue;
However, Sleep() functions executes before first line (painting red). Program starts from waiting 3 seconds and after time it paints the button blue. It seems like painting red piece of code doesn't have time to execute. Individually, painting red works fine.
I've tried other delay solutions also. Example:
int wait = clock() + 2 * CLOCKS_PER_SEC;
while (clock() < wait) {}
It seems to be an issue in Visual Studio C++, because the Sleep() function have worked perfectly in Code::Blocks console script. Do you have any ideas of solution?
Since Button is also a Window you can Invalidate it before calling Sleep().
what is invalidate,update methods do in VC++
Setting component properties, eg 'button1->BackColor = System::Drawing::Color::DarkRed;' are not trivial assignments. The setter methods generate messages, or sequences of messages, that are posted to the Windows/thread that implement the GUI. Those messages must be handled before the requested property set actions can be considered completed.
If you set a window visual property in an event-handler, and then remove all execution from the thread that manages the window before leaving the event-handler, the messages will not get processed.
Do not wait in a GUI event handler. It's a state-machine for handling messages. Don't stop it.
Thank you for your contribution guys!
Advice about threads was helpful. I've solved this problem with the Refresh() function. It seems like parts of code are handled with different threads which work asynchronous. Refresh() function probably implements waiting for the threads to by synchronised.

SendMessage WM_COPYDATA between two processes [duplicate]

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:
case ID_BUTTON_START:
// Code executed when pressing Start Button.
char startButtonText[30]; // Storing next loop text
for (int i=5; i>0; i--)
{
sprintf(startButtonText, "Starting in ... %d", i);
SendMessage(hwndButtonStart, WM_SETTEXT, 0, (LPARAM)(startButtonText));
Sleep(1000);
}
Is this normal? If not what's causing this?
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:
Create a new (worker thread) for the task.
If the task is likely to be done often, use a thread pool instead.
Set and subscribe to timer events.
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.

Any Way of Waiting for Console Unfocus or Pause Until Not Focused?

I have a simple problem that I am struggling to find an answer to. In my situation, I am attempting to wait for user input into the console to activate the main loop of the program, but the program can not be allowed to enter the main loop until the window behind it is in focus. If you do not understand what I mean, here is an example. I have two windows, the one in focus(on top) is the console running the program which is at a pause state while waiting for the window behind it to become the window on top and in focus. I think that either enabling the console to read keyboard input while not in focus or having the program recognize that it is no longer in focus would work. Is it possible to pull off either of those situations in Windows?
Thank you in advance.
I think "focus" doesn't mean what you think it means. "Pause state" on the other hand has no special meaning. If your program has a "pause state", then you defined that state, and you define when the state is entered. So just enter the pause state when you feel like it.
That said, the strict answer to your question is the WM_KILLFOCUS message, but that arrives in the message loop for a window and you don't own the message loop of a console window.

click close console window to end a c++ console program is proper way?

I have just got this problem for a few days. Before, I've always thought that letting the program exit by returning from main and clicking close the console window is the same way to end the program.
However, I've found that they are different. Since my program opens a camera which is an object. And closing the console windows does not destroy or clean up the object. So the next time I have error to open the camera again
I just need a confirm if this is true?
Then why only until now I can see the problem?
Closing a console window in Windows, kills the running program (or stack of running programs). Unless it has registered a handler for this event, it gets no chance to clean up. If you want solution, register a handler.
Hm, consulting the documentation, wait a few secs…
OK, look up SetConsoleCtrlHandler.
Closing a running console application will kill the process, not giving you the chance for any clean up code. I guess you could hook a windows message loop to trap the WM_CLOSE message and do proper cleanup, but at the end of the day, you just shouldn't kill the process.

Sleep() in Win32 makes program unresponsive

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:
case ID_BUTTON_START:
// Code executed when pressing Start Button.
char startButtonText[30]; // Storing next loop text
for (int i=5; i>0; i--)
{
sprintf(startButtonText, "Starting in ... %d", i);
SendMessage(hwndButtonStart, WM_SETTEXT, 0, (LPARAM)(startButtonText));
Sleep(1000);
}
Is this normal? If not what's causing this?
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:
Create a new (worker thread) for the task.
If the task is likely to be done often, use a thread pool instead.
Set and subscribe to timer events.
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.