1. What if I only want to sleep for milliseconds?
- Posted by tiger05 Jun 24, 2009
- 1314 views
Hello. I created a countdown timer program, but the program grinds the CPU to a halt when using Windows. I wanted to use the sleep function so the program wouldn't be constantly hogging the CPU, but the sleep function sleeps for seconds instead of milliseconds. Is there a way to only sleep for milliseconds in Euphoria like one could do in BASIC?
2. Re: What if I only want to sleep for milliseconds?
- Posted by ryanj Jun 24, 2009
- 1330 views
Hello. I created a countdown timer program, but the program grinds the CPU to a halt when using Windows. I wanted to use the sleep function so the program wouldn't be constantly hogging the CPU, but the sleep function sleeps for seconds instead of milliseconds. Is there a way to only sleep for milliseconds in Euphoria like one could do in BASIC?
In version 4, sleep() will allow milliseconds. If you are using 3.1, you can use the nanosleep() function in the idle.e library, which you can download here:
http://www.rapideuphoria.com/idlelib.zip
global procedure nanosleep(atom s) --Sleeps for s seconds. 0.000000001 s = 1 nanosecond. --This is compatible with the sleep() function. However, you can use non-integers as well. --For example: -- nanosleep(0.001) --sleeps for 1 millisecond -- nanosleep(0.000000001) --this is the smallest amount of time possible (linux) -- nanosleep(0) --sleeps for the smallest amount of time possible -- nanosleep(1.5) -- nanosleep(17.0003) -- nanosleep(0.5) ...
Hope that helps. Let me know if there are any problems.
~ryanj
3. Re: What if I only want to sleep for milliseconds?
- Posted by ghaberek (admin) Jun 24, 2009
- 1357 views
Are you writing a console or GUI application? If you're making a GUI, you should setup a timer to fire after "x" number of milliseconds and react via an event handler. If you simply call sleep() in a GUI program, the GUI will "lock up" until sleep() returns.
-Greg
4. Re: What if I only want to sleep for milliseconds?
- Posted by LarryMiller Jun 24, 2009
- 1351 views
Are you writing a console or GUI application? If you're making a GUI, you should setup a timer to fire after "x" number of milliseconds and react via an event handler. If you simply call sleep() in a GUI program, the GUI will "lock up" until sleep() returns.
-Greg
Very true. If this is a GUI app you really don't want to be using sleep(), except possibly for very short delays. During the sleep() perioed your app will be unable to respond to messages and will appear to have hung. In a GUI app not responding to messages is a very bad thing. In some cases this can even cause other aplications to hang. In almost every case using a timer is the better solution.
Larry Miller
5. Re: What if I only want to sleep for milliseconds?
- Posted by tiger05 Jun 24, 2009
- 1301 views
- Last edited Jun 25, 2009
It is a console application.
6. Re: What if I only want to sleep for milliseconds?
- Posted by tiger05 Jun 24, 2009
- 1286 views
- Last edited Jun 25, 2009
Sleep can accept milliseconds in version 4? I have the third alpha of version 4 and if I have something like sleep(0.1) I get a type check error.
7. Re: What if I only want to sleep for milliseconds?
- Posted by ryanj Jun 25, 2009
- 1299 views
It doesn't work? That's strange. I specifically remember having a chat with the developers about modifying the sleep() routine about...a year ago? I thought for sure they implemented the more precise version. What happened, Eu guys?
8. Re: What if I only want to sleep for milliseconds?
- Posted by DerekParnell (admin) Jun 25, 2009
- 1288 views
It doesn't work? That's strange. I specifically remember having a chat with the developers about modifying the sleep() routine about...a year ago? I thought for sure they implemented the more precise version. What happened, Eu guys?
It works fine for me. I'm using revision #2176 but I'm not sure what the latest alpha was using.
Have a look at include/std/os.e file for the sleep() function. It should look like this ...
public procedure sleep(atom t) -- go to sleep for t seconds -- allowing (on WIN32 and Linux) other processes to run if t >= 0 then machine_proc(M_SLEEP, t) end if end procedure
9. Re: What if I only want to sleep for milliseconds?
- Posted by mattlewis (admin) Jun 25, 2009
- 1320 views
It doesn't work? That's strange. I specifically remember having a chat with the developers about modifying the sleep() routine about...a year ago? I thought for sure they implemented the more precise version. What happened, Eu guys?
It works fine for me. I'm using revision #2176 but I'm not sure what the latest alpha was using.
Have a look at include/std/os.e file for the sleep() function. It should look like this ...
He probably was still using the 3.1 standard library. It's important to note that the new library needs to be prefixed by 'std/'.
Matt
10. Re: What if I only want to sleep for milliseconds?
- Posted by tiger05 Jun 25, 2009
- 1287 views
Thanks. I changed my include line to std/os.e instead of misc.e.
11. Re: What if I only want to sleep for milliseconds?
- Posted by CladStone Jun 25, 2009
- 1279 views
- Last edited Jun 26, 2009
Two answers may be plenty, but here's one more way for Win32 - this is the what I use.
Just link to kernel32.dll and use that function:
global constant cSleep = define_c_proc( kernel32, "Sleep", -- msdn2.microsoft.com/en-us/library/ms686298.aspx {DWORD}), -- milliseconds to sleep. -- A value of zero causes the thread to relinquish the remainder of its time slice -- to any other thread of equal priority that is ready to run. -- If there are no other threads of equal priority ready to run, -- the function returns immediately, and the thread continues execution.
Notes:
- Just wrap cSleep into whatever routine structure is convenient for you.
- DWORD = C_ULONG = C_UINT = #02000004 (see dll.e)
12. Re: What if I only want to sleep for milliseconds?
- Posted by ryanj Jun 25, 2009
- 1264 views
- Last edited Jun 26, 2009
Two answers may be plenty, but here's one more way for Win32 - this is the what I use.
Just link to kernel32.dll and use that function:
global constant cSleep = define_c_proc( kernel32, "Sleep", -- msdn2.microsoft.com/en-us/library/ms686298.aspx {DWORD}), -- milliseconds to sleep. -- A value of zero causes the thread to relinquish the remainder of its time slice -- to any other thread of equal priority that is ready to run. -- If there are no other threads of equal priority ready to run, -- the function returns immediately, and the thread continues execution.
Notes:
- Just wrap cSleep into whatever routine structure is convenient for you.
- DWORD = C_ULONG = C_UINT = #02000004 (see dll.e)
Yes, that is the exact routine that is called by eu4's sleep(). In linux, there is an equivalent function that is called. Also, by the way, multitasking now uses that when there is idle time between scheduled tasks. (Previously, less than 1 second of time between tasks would cause 100% CPU usage because the task scheduler used active waiting.) There is also a new multi-tasking funtion called task_delay().
One more thing: I noticed that the documentation for sleep() doesn't really put an emphasis on the fact that it now accepts non-integers. Don't you think it should tell exactly how precise it is now? I believe it is milliseconds in Windows, and nanoseconds in linux. I don't know what it is in DOS. Could someone add that to the docs, please?
13. Re: What if I only want to sleep for milliseconds?
- Posted by tiger05 Jun 26, 2009
- 1278 views
- Last edited Jun 27, 2009
I'll keep that in mind for the future, but I want my program to run on Mac/Linux so I will use the sleep function that comes with Euphoria 4.