Page 1 of 1

WaitWindowsEvent Loop

Posted: Wed Jul 21, 2010 9:05 am
by orb_505
Hi all,

Used PB for a while but mainly for internal programs at work. Now I'm using it for an 'entertainment' reason and I'm seeing a problem.

To give a brief run down I'm writing a simulation of a fictional space ship. I'm not interested at this stage with graphics or anything, I just want to simulate the actual systems of the ship itself (power, life support etc). So, I'm just using basic windows gadgets for controls and readouts.

The algorithims for how the the systems work are held in their own Procedures which I'm calling from the main WaitWindowsEvent Repeat:Until loop. The problem is that this loop only loops on a windows event.

So, for example, my Reactor get switched on. The core temperature then rises. The algorithim for the temp rising and how much is contained in in the ReactorSystems procedure and updates a progress bar on the Windows form.

Code: Select all

;Engineering
Procedure ReactorSystem()
  
  If ReactorOn
    If temp < 100
      temp = temp + 1
    EndIf
  Else
    If temp > 0
      temp = temp - 1
    EndIf
  EndIf
  
  SetGadgetState (#prgCoreTemp, temp)
EndProcedure

CreateThread(@ReactorSystem(),0)

;Main Loop
Repeat
  Event = WaitWindowEvent()  
    Select Event
    Case #PB_Event_Gadget
    Select EventGadget()
      Case #cmdReactorStart
        ReactorOn = 1  
      Case #cmdRecactorStop
        ReactorOn = 0
    EndSelect      
  EndSelect
    
ReactorSystem()

Until Event = #PB_Event_CloseWindow
What I want to happen is when I press the ReactorStart button the core temperature slowly and evenly rises. What actually happens is that the temperature only rises when I move the mouse (eg a Windows Event). If I don't move the mouse the temperature doesn't rise.

I tried using Threads, I assume I did it correctly as it didn't error, I've never used Threads before but it didn't help. (Please ignore the simple temperature rising algorithim, I kept it simple until I could get these basics working).

How should I be coding this to get what I need?

Cheers

Mark

Re: WaitWindowsEvent Loop

Posted: Wed Jul 21, 2010 9:35 am
by Arctic Fox
Have you tried using a timeout value with WaitWindowEvent() or perhaps replacing it with WindowEvent()?
Another solution is to use a screen - OpenScreen() or OpenWindowedScreen() - it is designed for animations, games etc. :)

Re: WaitWindowsEvent Loop

Posted: Wed Jul 21, 2010 10:05 am
by Vera
Hello orb,
how about inserting a loop into the procedure ?

Code: Select all

Global temp = 70
Procedure ReactorSystem()
 While temp < 100 And temp > 0
  If ReactorOn
    If temp < 100
      temp = temp + 1
    EndIf
  Else
    If temp > 0
      temp = temp - 1
    EndIf
  EndIf
;  SetGadgetState(#prgCoreTemp, temp)
Debug temp
Delay(200)
Wend
EndProcedure
greetings ~ Vera

Re: WaitWindowsEvent Loop

Posted: Wed Jul 21, 2010 10:17 am
by orb_505
WindowEvent() worked a treat ;) Cheers!

Re: WaitWindowsEvent Loop

Posted: Wed Jul 21, 2010 10:29 am
by Arctic Fox
orb_505 wrote:WindowEvent() worked a treat ;) Cheers!
Remember to use a Delay() somewhere in the loop - unless you want the simulation to steal all the CPU power on your computer :mrgreen: :wink:

Re: WaitWindowsEvent Loop

Posted: Wed Jul 21, 2010 11:25 am
by orb_505
I've put a Delay(5) in. It gives a nice slow progression to the temp build up and doesn't make the app look like it's locked because it's too high.

Cheers

Mark