Page 1 of 1

OOBasic timer without a do loop

PostPosted: Mon Mar 08, 2021 4:03 pm
by JeJe
There are several threads with people looking for an OOBasic timer... without an entirely satisfactory solution... and this probably isn't either... but its slightly different from putting a wait statement in a loop.

It requires a dialog and a commandbutton. The alternative to the loop statement is to keep firing the commandbutton execute from within the macro it calls. No global variables are needed to stop the execution.

Seems okay to me but may or may not be very stable.

Re: OOBasic timer without a do loop

PostPosted: Mon Mar 08, 2021 4:31 pm
by Villeroy
Same problem as with the do...wait...loop. There is no threading in StarBasic. The IDE is locked and you can not run other macros while the clock is running.

Re: OOBasic timer without a do loop

PostPosted: Mon Mar 08, 2021 4:54 pm
by JeJe
You can run other macros... you can do that with a do loop running as well.

Edit: if it was just a do loop without a wait statement then you couldn't run another macro while it was running but the wait statement yields. The trick I've used yields somewhere as well - the wait statement isn't needed for that - but the CPU usage goes from 0% to 50% on mine without it.

The problem with wait and waituntil is you might have tried closing the application down or something in the middle of the wait. And with the loop, its getting out of it.

(I've just run a test with doevents in a do loop and that doesn't allow another macro to run)

Re: OOBasic timer without a do loop

PostPosted: Mon Mar 08, 2021 6:03 pm
by Villeroy
You are right. It is possible. I failed running code from the IDE while the clock was running. It is possible from the organizer or from Tools>Macros>Run.

Re: OOBasic timer without a do loop

PostPosted: Mon Mar 08, 2021 6:12 pm
by JeJe
Yeah, you're right, there's a difference between running from the IDE and otherwise. Running my Doevents test from Tools menu, Doevents does also allow other macros to run.

Edit: the dialog can be modal or non modal - I've just used the easiest to create.

Re: OOBasic timer without a do loop

PostPosted: Tue Mar 09, 2021 1:47 pm
by JeJe
Adapting the code back to the loop method needs something like below.
I've had problems with the code still running with the do/wait/loop method when the dialog has been closed. It needs an error handler and a test of dialog visibility.

Code: Select all   Expand viewCollapse view

Sub Main
dlg  = loaddialog("Standard","Dialog2")
'START THE TIMER BY CALLING EXECUTE METHOD OF THE COMMAND BUTTON USING THE ACCESSIBLECONTEXT
dlg.getcontrol("CommandButton1").accessiblecontext.doaccessibleaction(0)
dlg.execute
End Sub

sub fnt2(ev) 'CALLED ON THE COMMANDBUTTON EXECUTE
dim t,mm,dlg,vis
dlg = ev.source.getcontext
on error goto hr
mm= ev.source.getcontext.model
do
vis =  dlg.isvisible
if vis =false then exit do
t = time
if mm.title<> t then mm.title =t
wait 500
loop
hr:
end sub