0

I want to know how can I use async sleep for fixing problem. Can it be done in C++? Someone help me please :)

Problem description

  1. Print Hello world

  2. Sleep(20000) <- I can't do anything

  3. WM_CLOSE


How can I fix it?

cout << "Hello World!";
Sleep(20000);
::PostMessage(::GetConsoleWindow(), WM_CLOSE, 0, 0);

I'm noob, please tell me your answer more easier.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
박혜인
  • 39
  • 3
  • What do you want to do? You say _I can't do anything_ during the `Sleep` call, but you haven't explained what you actually want to do. – Matt Hogan-Jones Apr 28 '17 at 10:15
  • Look here: http://stackoverflow.com/questions/14650885/how-to-create-timer-events-using-c-11 – Marco Apr 28 '17 at 10:16
  • Sleeping asynchronously makes no sense. You are asking to have some other thread to do nothing for some time. – Passer By Apr 28 '17 at 10:25
  • 1
    You could use `SetTimer` and catch the `WM_TIMER` message in the message loop. – alain Apr 28 '17 at 10:26
  • What would you like the user to be able to do during those 20 seconds? For simple console apps the usual method is to write something like "Press Enter to end...", read a char, and then just `return` from `main`. – Dialecticus Apr 28 '17 at 10:35
  • i want to do everything but my mouse don't move and my keyboard don't work – 박혜인 Apr 28 '17 at 11:05

1 Answers1

0

Sleep(n) tells your process to do nothing for n seconds. Doing nothing means also not accepting any input. As you want to close your window, do it the other way round. Start an async process which waits for n seconds before it calls the close-this-window method. Or much better: Schedule an event to take place n seconds in the future which closes the window immediately after it fires.

DBX12
  • 2,041
  • 1
  • 15
  • 25