It is currently Wed Jun 19, 2013 2:28 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [beginner question] Using Delay
PostPosted: Thu Mar 15, 2012 10:33 am 
Offline
User
User

Joined: Wed Nov 09, 2011 8:58 am
Posts: 18
Hi, how can I make my program "sleep" between 2 gui updates but still be responsive (to a window event, for instance)?

In the short program below, I display 'x=0' then, after 5 seconds, 'x=1'. However during those 5 seconds, the window is not fully responsive, the hourglass cursor is shown (due to the 'Delay' instruction) and the window cannot be properly closed if needed. Is there another command than 'Delay' that I should be using in this case?

Thanks!

Code:

OpenWindow(0, 0, 0, 200, 200, "TestBox", #PB_Window_SystemMenu)

     text$ = "x=0"
       
TextGadget(4, 10,100,180,20,text$,#PB_Text_Center)

     Delay (5000)
             
     text$ = "x=1"
     
TextGadget(4, 10,100,180,20,text$,#PB_Text_Center)

Delay (2000)
   
 ; Exit


Top
 Profile  
 
 Post subject: Re: [beginner question] Using Delay
PostPosted: Thu Mar 15, 2012 11:17 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 821
Location: Germany
In order to keep your window responsive you have to implement
an event loop. In your example you don't look for events at all. I
have modified your example to use a timer which counts up your
x value every second (1000 ms). In that way your window and
gadgets allways stay responsive. Another point is that in order
to display text in a TextGadget you don't have to recreate that
gadget but instead use SetGadgetText() to update the content
of your TextGadget:
Code:
OpenWindow(0, 0, 0, 200, 200, "TestBox", #PB_Window_SystemMenu)
TextGadget(0, 10, 100, 180, 20, "", #PB_Text_Center)
SetGadgetText(0, "x=0")
AddWindowTimer(0, 0, 1000)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      RemoveWindowTimer(0, 0)
      Break
    Case #PB_Event_Timer
      x + 1
      SetGadgetText(0, "x=" + Str(x))
  EndSelect
ForEver


Top
 Profile  
 
 Post subject: Re: [beginner question] Using Delay
PostPosted: Thu Mar 15, 2012 11:29 am 
Offline
User
User

Joined: Wed Nov 09, 2011 8:58 am
Posts: 18
Fantastic! Thank you very much.


Top
 Profile  
 
 Post subject: Re: [beginner question] Using Delay
PostPosted: Sat Mar 17, 2012 11:27 am 
Offline
User
User

Joined: Wed Nov 09, 2011 8:58 am
Posts: 18
I've got a related question... What if the program is console-based (no GUI)?
How can I make it sleep for a few seconds or minutes, without taking 100% CPU?
I suppose event timers are not the way to go in that case, correct?


Top
 Profile  
 
 Post subject: Re: [beginner question] Using Delay
PostPosted: Sat Mar 17, 2012 11:56 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1468
Location: Germany
Hi,

than you need Delay().
for an example look at the help for Inkey()

Bernd


Top
 Profile  
 
 Post subject: Re: [beginner question] Using Delay
PostPosted: Sat Mar 17, 2012 12:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 560
The best way is to create a loop that checks the time using ElapsedMilliseconds() in every iteration.
A simple Delay(0) or longer prevents the loop from consuming too much processor power.

The advantage is that you still have control over the loop and you can abort it when you need.

_________________
Image
ImageImageImage
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye