|
Author |
Topic: 0046: delay() in loop (Read 317 times) |
|
Glen Murphy
|
0046: delay() in loop
« on: Dec 4th, 2002, 4:35am » |
|
Having delay() inside loop, delays the opening of the applet window, while this is unsuprising, given that the window probably isn't created until after the first run of loop(), you still get delays if delay() is present. See the following code: //// int x = 0; void setup() { size(200,200); } int first = 2; void loop() { rect(x*10,x*10, 10, 10); if(first > 0) first--; else delay(10000); } //// (The count-down first thing is because you get longer delays if it's just a binary 0/1). If you take out the if/else lines, it runs fine, but a bit fast. Probably not a 'real' issue, but one that will probably confuse people (and create applets that seemingly take forever to load). (edit: added version number to title, for future reference)
|
« Last Edit: Dec 9th, 2002, 6:00am by Glen Murphy » |
|
|
|
|
Glen Murphy
|
Re: delay() in loop
« Reply #1 on: Dec 4th, 2002, 4:45am » |
|
For those interested, I work around this with something horribly kludgy like: //// int tempus = -20000; void loop() { if(millis() > tempus + 10000) { tempus = millis(); create(); display(); } } //// It's also interesting (and inexplicably bizarre) to note that this only sucks up 70% of my CPU time, as opposed to 97% when using the delay() method.
|
|
|
|
fry
|
Re: delay() in loop
« Reply #2 on: Dec 5th, 2002, 6:21am » |
|
that's annoying with delay. thanks for the report.. it's probably enough of a problem that a workaround should be built in. the reason for the cpu jump is that the loop on delay is too tight.. the code from the applet's base class is: Code: public void delay(int howlong) { long stop = System.currentTimeMillis() + (long)howlong; while (System.currentTimeMillis() < stop) { } return; } |
| and it probably needs to sleep the thread otherwise it slurps up too much cpu (the 97% is the rest of the processes on the machine being starved of resources). i think i wrote the method in a quick few minutes and didn't give it any thought.. oops.. and i rarely use delay so i hadn't come across the problem. i'll see if i can't get a fix in for 47. i'd like to avoid making everyone write 'standard' kludges (where the p5 "in crowd" are those who know the longest set of hacks to get around problems).. so the things we can find i'd prefer to fix, at least before we freeze up the api and features. so.. keep digging everyone! thanks again for the note..
|
« Last Edit: Dec 5th, 2002, 6:21am by fry » |
|
|
|
|
fry
|
Re: 0046: delay() in loop
« Reply #3 on: Jan 26th, 2003, 10:43pm » |
|
this should now be fixed in rev 49. let us know how it's working.
|
|
|
|
|