1. What if I only want to sleep for milliseconds?

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?

new topic     » topic index » view message » categorize

2. Re: What if I only want to sleep for milliseconds?

tiger05 said...

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: What if I only want to sleep for milliseconds?

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: What if I only want to sleep for milliseconds?

ghaberek said...

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: What if I only want to sleep for milliseconds?

It is a console application.

new topic     » goto parent     » topic index » view message » categorize

6. Re: What if I only want to sleep for milliseconds?

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.

new topic     » goto parent     » topic index » view message » categorize

7. Re: What if I only want to sleep for milliseconds?

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?

new topic     » goto parent     » topic index » view message » categorize

8. Re: What if I only want to sleep for milliseconds?

ryanj said...

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 
new topic     » goto parent     » topic index » view message » categorize

9. Re: What if I only want to sleep for milliseconds?

DerekParnell said...
ryanj said...

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

new topic     » goto parent     » topic index » view message » categorize

10. Re: What if I only want to sleep for milliseconds?

Thanks. I changed my include line to std/os.e instead of misc.e.

new topic     » goto parent     » topic index » view message » categorize

11. Re: What if I only want to sleep for milliseconds?

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)

new topic     » goto parent     » topic index » view message » categorize

12. Re: What if I only want to sleep for milliseconds?

CladStone said...

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?

new topic     » goto parent     » topic index » view message » categorize

13. Re: What if I only want to sleep for milliseconds?

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu