Loops and events

Find quick help here to get you started with Hollywood
Post Reply
zylesea
Posts: 227
Joined: Tue Feb 16, 2010 12:50 am
Location: Westfalen/Germany
Contact:

Loops and events

Post by zylesea »

Making a program asynchronous gives me some headaches and gives me the impression like really stuck in the _very_ basic stuff... :?

I want a loop that gets executed (at max speed) once a (MUI) button got pressed until the stop (MUI) button got pressed.

Here's the code how I thought it would work:

Code: Select all

@REQUIRE "MUIRoyale"
@FILE 1, "async.xml"
@DISPLAY {Hidden = True}   ;{width=200, height=200};{Hidden = True}

Function p_start()
    While my_quit$= True
           counter$=counter$+1
           DebugPrint("counter", counter$)
      Wend
EndFunction

Function p_EventFunc(msg)
	Switch msg.action
	Case "MUIRoyale":
        		Switch msg.attribute
	       	    Case "CloseRequest":
			         End
		        Case "Pressed":
                  Switch  msg.id
                  Case "mybt2":
                    my_quit$= True
                    p_start()
                  Case "mybt1":
                    my_quit= False
                  EndSwitch
                EndSwitch
    EndSwitch
EndFunction

mui.createGUI(ReadString(1, FileLength(1)))

InstallEventHandler({MUIRoyale= p_EventFunc})
SetInterval(1, p_EventFunc, 10)

Repeat
WaitEvent
Forever     
for completeness the according xml fle:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<application base="Async" id="app">
<menustrip id="mymenu">
<menu title="Project">
<item id="menaboutmui" notify="selected" userdata="_void_">About MUI...</item>
<item id="menaboutmuiroyale" notify="selected" userdata="_void_">About MUI Royale...</item>
<item/>
<item id="menquit" notify="selected"  userdata="_void_" shortcut="Q">Quit</item>
</menu>
<menu title="Settings">
<item id="menmuiset" notify="selected"  userdata="_void_">MUI...</item>
</menu>
</menustrip>
	<window title="Async" muiid="MAIN" notify="closerequest" menustrip="mymenu" >
			<vgroup>
            <hgroup>
              <button id="mybt2" notify="pressed" shorthelp="Rename by Exif data">Start</button>
              <button id="mybt1" notify="pressed" shorthelp="Stop current operation">Stop</button>
			</hgroup>
		</vgroup>
	</window>
</application>   
But it doesn't work. the loop is not left when pressing stop, but just continues.
Is there a way to actively poll a MUI button state? I thought ofsomething like mui.get("mybt2","Pressed") - but it's not working, too.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Loops and events

Post by airsoftsoftwair »

CheckEvent() is your friend.
zylesea
Posts: 227
Joined: Tue Feb 16, 2010 12:50 am
Location: Westfalen/Germany
Contact:

Re: Loops and events

Post by zylesea »

Somehow i am lost with the right usage of checkevent.
I didn't got it how to use it with my examle.
Do i need to replace waitevent with checkevent? But this doesn't change anything except cpu usage is going 100% (well, it's checking events all the time), but once in the while loop events are not checked any longer.

checkevent within the while loop (I firstly thought to use it that way) is also wrong as it is within the waitevent callback function then and doesn't work.

So far i came to conclusion that it seems to be better to do it like in my other programs and open the while..wend loop to a periodically executed condition (if..endif) :

Code: Select all

@REQUIRE "MUIRoyale"
@FILE 1, "async.xml"
@DISPLAY {Hidden = True}  
my_quit$=False

Function p_start()
    If my_quit$= True
           counter$=counter$+1
           DebugPrint("counter", counter$)
      EndIf
EndFunction

Function p_EventFunc(msg)
	Switch msg.action
	Case "MUIRoyale":
        		Switch msg.attribute
	       	    Case "CloseRequest":
			    End
		        Case "Pressed":
                  Switch  msg.id
                  Case "mybt2":
                    my_quit$= True
                  Case "mybt1":
                    my_quit$= False
                  EndSwitch
                EndSwitch
    EndSwitch
EndFunction

mui.createGUI(ReadString(1, FileLength(1)))
InstallEventHandler({MUIRoyale= p_EventFunc})

Function p_main()
    p_start()
EndFunction

SetInterval(1, p_main, 10)
Repeat
WaitEvent
Forever  
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Loops and events

Post by airsoftsoftwair »

Oops, sorry, you're right. Hollywood 6.0 blocks CheckEvent() calls made from callbacks. This doesn't really make sense because you might want to run modal loops in a callback and I removed this limitation for Hollywood 6.1 some months ago. Until then, you have to workaround the problem. WaitEvent() will exit whenever it ran a callback so when "mybt2" has been pressed you could set a flag that calls p_start() manually after WaitEvent() has exited. Then you should be able to use CheckEvent() in p_start() e.g.

Code: Select all

@REQUIRE "MUIRoyale"
@FILE 1, "async.xml"
@DISPLAY {Hidden = True}   ;{width=200, height=200};{Hidden = True}

Function p_start()
    While my_quit$= True
           counter$=counter$+1
           CheckEvent
           DebugPrint("counter", counter$)
      Wend
EndFunction

Function p_EventFunc(msg)
   Switch msg.action
   Case "MUIRoyale":
              Switch msg.attribute
                 Case "CloseRequest":
                  End
              Case "Pressed":
                  Switch  msg.id
                  Case "mybt2":
                    my_quit$= True
                    flag=True
                  Case "mybt1":
                    my_quit= False
                  EndSwitch
                EndSwitch
    EndSwitch
EndFunction

mui.createGUI(ReadString(1, FileLength(1)))

InstallEventHandler({MUIRoyale= p_EventFunc})
SetInterval(1, p_EventFunc, 10)

Repeat
WaitEvent
If flag = True
   p_start()
   flag = False
EndIf
Forever  
This should do the trick.
zylesea
Posts: 227
Joined: Tue Feb 16, 2010 12:50 am
Location: Westfalen/Germany
Contact:

Re: Loops and events

Post by zylesea »

Thanks. That works indeed.
Post Reply