OOBasic timer without a do loop

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

OOBasic timer without a do loop

Post 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.
Attachments
timer.odt
(12.68 KiB) Downloaded 311 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OOBasic timer without a do loop

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic timer without a do loop

Post 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)
Attachments
timer.odt
(12.75 KiB) Downloaded 307 times
Last edited by JeJe on Mon Mar 08, 2021 6:09 pm, edited 1 time in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OOBasic timer without a do loop

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic timer without a do loop

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic timer without a do loop

Post 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


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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply