sleep

Why is the sleep-time of Sleep(1) seems to be variable in Windows?

痞子三分冷 提交于 2019-12-12 17:24:16
问题 Last week I needed to test some different algorithmic functions and to make it easy to myself I added some artifical sleeps and simply measured the clock time. Something like this: start = clock(); for (int i=0;i<10000;++i) { ... Sleep(1); ... } end = clock(); Since the argument of Sleep is expressed in milliseconds I expected a total wall clock time of about 10 seconds (a big higher because of the algorithms but that's not important now), and that was indeed my result. This morning I had to

Console.ReadLine Break

孤街醉人 提交于 2019-12-12 15:14:41
问题 I'm trying to figure out how I can use Console.ReadLine and a timer. My console program is designed to run a long process automatically, with this process restarting every 30 seconds after the previous process completed. I want to give the user the ability to break the auto-run by typing a command though. If I use Console.ReadLine() though, it will wait until the user enters something, whereas I want the program to continue on through a loop if nothing is entered within 30 seconds. . . Any

Using Sleep() while using timers through setitimer

偶尔善良 提交于 2019-12-12 14:07:57
问题 I am using a timer in my C++ code through setitimer function from sys/time.h. This maps the SIGALRM signal to my timer handler method. After this I am not able to use sleep function. I am assuming it is because sleep uses SIGALRM signal as well. Can you suggest any workaround for this problem? Thanks for replying. 回答1: You can try using select() just as a timer. I don't know if it uses SIGALRM or not but it should be simple to test. Something like: timeval t = {1, 0}; select(0, NULL, NULL,

NSTimer continue during sleep mode

Deadly 提交于 2019-12-12 14:01:06
问题 I have a timer app which uses a NSTimer to track minutes. How can I keep the timer going, even when the device is put into sleep mode? Thanks! 回答1: Adjust After Wake Notification Scheduled NSTimer instances will halt during sleep. Your application needs to respond to the NSWorkspace's wake notification and adjust its timers. If you do not respond to wake notifications, your timers will resume but without accounting for the time spent asleep. See Apple's technical note QA1340 Registering and

Using sleep and select with signals

故事扮演 提交于 2019-12-12 13:35:10
问题 I want to use the select() function to wait for 1 second, as my program uses signals to control stuff, so sleep() would return prematurely. The weird thing is that when using select() it also returns prematurely. I am calling select like this struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 1000000; select (0 ,NULL, NULL, NULL, &timeout); but whenever a signal arrives, it returns (I am using a nano second timer for the signal) Anyone knows why? 回答1: Try something like this:

Does adding sleep() to a loop alleviate server resources?

那年仲夏 提交于 2019-12-12 13:09:29
问题 I'm running a PHP loop that 'scans' a directory, every 60 seconds, until a file with a given name is found: <?php do { if (file_exists("../path/file.txt)) { //Do Stuff $status = "File Found"; echo $status; } else { $status = "File Not Found"; sleep(60); } } while ($status == "File Not Found"); ?> In this example, would removing sleep() require more server resources? Thank you, 回答1: In a nutshell, yes, but don't worry about it. While sleep is executing, CPU processing of your script virtually

Java Thread.sleep leaking threads?

隐身守侯 提交于 2019-12-12 12:16:44
问题 So I inherited a bit of code that's waiting for communication from a network source. While it's waiting for more data from the network socket, Thread.sleep(10) is called. This appears to be causing a thread leak, as reported by jconsole and my thread dump here (there are hundreds of entries for Thread-68, Thread-385, etc... but I shortened for brevity): Wed Jan 18 09:14:40 PST 2012 2012-01-18 09:14:50 Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.0-b11 mixed mode): "Thread-69" daemon

Any difference between kernel32.dll Sleep and Thread.Sleep()

陌路散爱 提交于 2019-12-12 11:01:27
问题 Is there any difference(performance, implementation. .whatever) between the following: i) DllImport("kernel32.dll")] public extern static void Sleep(uint msec); ..then call Sleep function ii) Thread.Sleep() 回答1: There's a big difference, actually. This blog post explains why managed threads should never do unmanaged blocking, if possible. The official MSDN documentation has the same guideline without all the underlying details. P.S. Thread.Sleep is a sign of a poorly-designed program. 回答2: I

Pause a thread for less than one millisecond

£可爱£侵袭症+ 提交于 2019-12-12 09:32:32
问题 In a messaging client test application, the producer thread needs to be throttled to avoid flooding the server. As the transfer rates are around 25,000 messages per second (40 microseconds per message), the delay caused by Sleep(1) would far too long. How to make thread sleep less than a millisecond on Windows contains some information related to the Windows API. Is there a code snippet, class or library for Delphi? Following Bens answer I found that Sleep with different values below 15 also

Pausing execution of a Thread WITHOUT sleeping?

橙三吉。 提交于 2019-12-12 09:05:50
问题 I am using the Skype API, which sends back a message everytime it receives one. I am not sure if this really is what is causing it, but it's the closest I can get: When I send too many messages, the COM control can't handle all the replies, which causes it to crash the whole app. That happens when I use a for loop. I am using Threads to do the job, so my program won't hang. I know I can do Sleep(); in the thread, and will (should) not make the whole program sleep. The problem is though, that