NanoToolkit Blog

The Way to Keep in Touch.

Need a Windows Command to Sleep.

Unfortunately on Windows there is no Way to run a command that simply returns after a specified number of milliseconds. This is a much needed Command that is very easy to create for a developer using theWindows Sleep API. Basically Windows needs a Command line .EXE that is called “delay” which would return after X number of milliseconds that the user specifies. It could look something like this in action if you wanted “delay” command to delay the execution by 500 milliseconds.

C:\Users\ehsan>delay 500

So one might ask what is the purpose of doing this? Well imagine some use cases as follows. I could use “Delay” command to wait before executing the next command.

Normally I could run a command like this stop a windows service.

C:\Users\ehsan samani>sc stop winmgmt

But the problem is sc command is asynchronous in nature which means it returns immediately and does not wait for the command to finish.

Wouldn’t it be nice to have a command that simply counts 500 milliseconds before it returns?

Because in that case I can simply run something like this.

C:\Users\ehsan>sc stop winmgmt

C:\Users\ehsan>delay 500

C:\Users\ehsan >sc query winmgmt

But currently most people use a hacked version of ping command to cause the command line pause for a period of time.

Check out this ping page on how to use ping to sleep in the command line interface.