Ambiguous Manual--OnEvent>Custom

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Ambiguous Manual--OnEvent>Custom

Post by armsys » Sun Nov 01, 2015 11:52 am

Referring to the manual:
OnEvent>Custom,SubroutineName,VariableName,Subroutine
Custom Trigger - runs given subroutine periodically and triggers when specified variable = TRUE

Code: Select all

OnEvent>CUSTOM,MyTriggerSub,DoIT,DoSomething
If>OK=1
  Gosub>MyTriggerSub
Endif

SRT>MyTriggerSub
  //This custom trigger monitors a registry key value
  RegistryReadKey>HKEY_CURRENT_USER,Software\MySoft,MyValue,res
  If>res=FIRE
    Let>DoIT=TRUE
  Endif
END>MyTriggerSub
SRT>DoSomething
  //reset the registry key and do something
  RegistryWriteKey>HKEY_CURRENT_USER,Software\MySoft,MyValue,IDLE
  //do something else
  Let>DoIT=FALSE
END>DoSomething
Questions:
1. Does OnEvent> runs the MyTriggerSub periodically?
2. How periodically? Every 5 seconds? Every 10 seconds? Every 30 seconds?...etc.
3. Can other part of code call MyTriggerSub such as Line 3 above still trigger the OnEvent?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Ambiguous Manual--OnEvent>Custom

Post by armsys » Sun Nov 01, 2015 9:33 pm

Referring to the screenshot, I am stunned by the discovery of:
1. Upon Line 1, subroutine MyTriggerSub is immediately and repeatedly executed.
2. Line 2 and the rest are never executed.
Is this normal?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Ambiguous Manual--OnEvent>Custom

Post by Marcus Tettmar » Tue Nov 03, 2015 11:30 am

Your code IS still being executed, but the way you've set it up it may be hard to spot. Try this example in the debugger instead. Step through it and you'll see the code in the main part of the script - the loop - is still executed. The trigger test subroutine is checked between every line:

Code: Select all

OnEvent>CUSTOM,MyTriggerSub,DoIT,DoSomething

Let>k=0
Repeat>k
  Wait>0.2
  Let>k=k+1
Until>k=200

SRT>MyTriggerSub
   // check something
END>MyTriggerSub

SRT>DoSomething
  // do something
END>DoSomething
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Ambiguous Manual--OnEvent>Custom

Post by armsys » Tue Nov 03, 2015 10:39 pm

Marcus Tettmar wrote:The trigger test subroutine is checked between every line.
Hi Marcus,
Your Revelation "The trigger test subroutine is checked between every line" is the key answer I was looking for.
Thank you.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Ambiguous Manual--OnEvent>Custom

Post by Marcus Tettmar » Wed Nov 04, 2015 9:29 am

That's not actually entirely true. It's checked all the time, running in a separate thread. But when you step through with the debugger it may appear as if it is checked between lines because of the way you'll see execution jump back and forth.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Ambiguous Manual--OnEvent>Custom

Post by armsys » Thu Nov 05, 2015 4:29 am

For those users who wish to gain appreciation of the awesome power of OnEvent>Custom, I wrote the following code for my hands-on experiment. The last few lines of the log shows before SRT>DoSomething can even (have chance and time to) execute EXIT (the last command), MyTriggerSub has already called at least once again. Indeed, just like Marcus strongly stresses, the MyTriggerSub is being called and executed VERY FREQUENTLY.
Thank you Marcus.

Code: Select all

Let>WLN_ENCODING=UNICODE
Let>k=0
Let>DoIt=False
OnEvent>CUSTOM,MyTriggerSub,DoIT,DoSomething

Repeat>k
  Wait>0.2
  Let>k=k+1
Until>k=200

SRT>MyTriggerSub
  Hour>Hr
  Min>Min
  Sec>Sec
  Hour>Hr

  WriteLn>C:\Temp\MyTriggerLog.txt,Result,MyTriggerSub...: %hr%:%Min%:%Sec% k=%k% DoIt=%DoIt%
  If>k>5
    WriteLn>C:\Temp\MyTriggerLog.txt,Result,MyTriggerSub...: %hr%:%Min%:%Sec% k=%k% DoIt=%DoIt% (If>k>5)
    Let>DoIt=TRUE
  Endif
  // check something
END>MyTriggerSub

SRT>DoSomething
  WriteLn>C:\Temp\MyTriggerLog.txt,Result,DoSomething....: %hr%:%Min%:%Sec% k=%k% DoIt=%DoIt%
  Exit
END>DoSomething

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts