Delay .. screen wait ... | SoloLearn: Learn to code for FREE!


Delay .. screen wait ...

How to make the execution wait in C++ for about 2 seconds ? I want the user wait on the screen and want the next line to display after about 2 seconds ? Is there some command for it ? please tell syntax and header file . I know that this could be done by looping but it will then wait differently every time .So need a better method .


4/10/2017 6:12:32 PM

Sahaj Bamba

10 Answers

New Answer


@Sahaj You’ll need to start by downloading and installing AutoHotkey, which is a simple scripting language that allows you to create easy scripts. Once you do that, right-click anywhere and choose New –> AutoHotkey Script. Once you’ve done that, paste the following into the script: #Persistent SetTimer, PressTheKey, 1800000 Return PressTheKey: Send, {Space} Return This simple script will wait every 30 minutes and press the Spacebar. You can adjust the 1800000 number above to the amount of milliseconds required. So, for example, if you wanted it to run every 2 minutes, you’d use 60 seconds * 2 minutes * 1000 milliseconds = 120000 total milliseconds. Save the file name as whatever you’d like, and then double-click on it to run it. You can also send another hotkey or any number of characters just by changing the Send, {Space} line to something else—you can literally type out some letters you want to send, or you can use some of the special keys on the AutoHotkey documentation page. For example, to make it send the word “lazy” and then press the Space bar, you could use: Send,lazy{Space}


I made it in Java though!


Simple method for Windows, include <windows.h> and do Sleep(2000). Console pauses for 2000ms = 2 seconds.


1 save the time at user input 2 loop through an empty while until the difference between the current time and the time of the input equals 2secons or 2000ms 3 continue your program as you like after the time wasting while loop #include <time.h> clock() gives you the current time in ms


Thanks everyone . @seamiki Your third method is the best i will be using that in my program . Thanks.😊


@seamiki Well i had asked for help from dayve on a project and he posted it here so to thank him i gave him best answer Xp .


@Sahaji Bamba if so, you could have marked it as best answer. Anyway glad it was helpful.


You can try sleep_for, from <thread>, using time units from <chrono>. More here (simple example given):