How do I get my program to sleep for 50 milliseconds?
Use time.sleep()
from time import sleep
sleep(0.05)
Sleep for milliseconds
Note that there is no standard C API for milliseconds, so (on Unix) you will have to settle for usleep
, which accepts microseconds:
#include <unistd.h>
unsigned int microseconds;
...
usleep(microseconds);
How do I get my C# program to sleep for 50 milliseconds?
System.Threading.Thread.Sleep(50);
Remember though, that doing this in the main GUI thread will block your GUI from updating (it will feel "sluggish")
Just remove the ;
to make it work for VB.net as well.
how can i make my program sleep for some amount of time?
On a POSIX platform, you can use select()
/poll()
on standard input and with a timeout parameter:
#include <sys/select.h>
#include <unistd.h>
int main(void)
{
fd_set inp; FD_ZERO(&inp); FD_SET(STDIN_FILENO,&inp);
select(1,&inp,0,0,&(struct timeval){.tv_sec=1});
}
With signal handlers on you'll also need to watch out for EINTR
and redo the request with the remaining time.
Unfortunately, you cannot count on either select()
or poll()
to give you the remaining time (select
can and will on Linux, through the struct timeval*
parameter, but the behavior isn't portable), so you'll need to use clock_gettime
to calculate it yourself.
Is there an alternative sleep function in C to milliseconds?
Yes - older POSIX standards defined usleep()
, so this is available on Linux:
int usleep(useconds_t usec);
DESCRIPTION
The usleep() function suspends execution of the calling thread for
(at least) usec microseconds. The sleep may be lengthened slightly by
any system activity or by the time spent processing the call or by the
granularity of system timers.
usleep()
takes microseconds, so you will have to multiply the input by 1000 in order to sleep in milliseconds.
usleep()
has since been deprecated and subsequently removed from POSIX; for new code, nanosleep()
is preferred:
#include <time.h>
int nanosleep(const struct timespec *req, struct timespec *rem);DESCRIPTION
nanosleep()
suspends the execution of the calling thread until either at least the time specified in*req
has elapsed, or the
delivery of a signal that triggers the invocation of a handler in the
calling thread or that terminates the process.The structure timespec is used to specify intervals of time with nanosecond precision. It is defined as follows:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
An example msleep()
function implemented using nanosleep()
, continuing the sleep if it is interrupted by a signal:
#include <time.h>
#include <errno.h>
/* msleep(): Sleep for the requested number of milliseconds. */
int msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0)
{
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
How to sleep for few milliseconds in swift 2.2?
usleep() takes millionths of a second
usleep(1000000) //will sleep for 1 second
usleep(2000) //will sleep for .002 seconds
OR
let ms = 1000
usleep(useconds_t(2 * ms)) //will sleep for 2 milliseconds (.002 seconds)
OR
let second: Double = 1000000
usleep(useconds_t(0.002 * second)) //will sleep for 2 milliseconds (.002 seconds)
Related Topics
Loop That Also Accesses Previous and Next Values
How to Get My Program to Sleep for 50 Milliseconds
Is the Time-Complexity of Iterative String Append Actually O(N^2), or O(N)
How to Get the Position of a Character in Python
How to Intercept Calls to Python's "Magic" Methods in New Style Classes
How to Choose Cross-Entropy Loss in Tensorflow
How to Open Every File in a Folder
Does Python Support MySQL Prepared Statements
Large, Persistent Dataframe in Pandas
Text with Unicode Escape Sequences to Unicode in Python
Class Method Decorator with Self Arguments
Is There a Short Contains Function for Lists
Why Is My Pygame Display Not Responding While Waiting for Input
Sorting Columns in Pandas Dataframe Based on Column Name