Page 1 of 1

Repeat Timer

Posted: Tue May 22, 2018 2:30 pm
by ace16vitamine
Dear all,

I need a simple counter to send every 30 Sec. emails (dosendmail) from a database query (dostartreading).

The think is: After 30 Sec. the counter is over and it will not repeat . :roll:

Any ideas how to repeat the counter, if currentTimerValue = 0 ?

Thanks
Stef




on declare_timer
global pTime
put 30 into pTime
put pTime into fld "fldCount"
countDownTimer pTime
end declare_timer

on countDownTimer currentTimerValue
subtract 1 from currentTimerValue
put currentTimerValue into fld "fldCount"
if currentTimerValue > 0 then
send "countDownTimer" && currentTimerValue to me in 1 sec

end if

if currentTimerValue = 0 then
dostartreading
global tRecords, tRecords2
put tRecords into field "Data"
dosendmail

end if


end countDownTimer

Re: Repeat Timer

Posted: Tue May 22, 2018 4:07 pm
by dunbarx
Hi.

I guess you want a repeating timer? Put this in a button script:

Code: Select all

on mouseup
   startTimer
end mouseup

on startTimer
   if the optionKey is down then exit to top
   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 1 second
end startTimer
I made the timing 1 second just so you can hear it work. I made the optionKey a way to escape, but this can be any other condition that is tested each iteration.

Because we are using "send in time", this process will run in the "background" as long as the current session is open, and whether or not LC is in front.

Craig Newman

Re: Repeat Timer

Posted: Tue May 22, 2018 6:59 pm
by jacque
Is this what you want?

Code: Select all

if currentTimerValue = 0 then 
  sendEmail
  send "declare_timer" to me in 0
else 
  send "countDownTimer" && currentTimerValue to me in 1 sec
end if 

Re: Repeat Timer

Posted: Tue May 22, 2018 7:47 pm
by ace16vitamine
Ok... Lets explain what I need.

I need to send Emails every X Minutes. To activate this setting I have a Button (Button Start). But sometimes I need to stop this Job, for this I have another Button (Button Stop).

Here is the code for Button Start:

Code: Select all


on mouseUp pButtonNumber
      global stopcountdown
      put 0 into stopcountdown -- This is the that the if Condition is working
 startTimer
 end mouseUp

Code: Select all

on startTimer
   beep 2 --Or sending Email
   send "startTimer" to me in 4 second --Do this every 4 seconds
   if stopcountdown is  1 then exit to top -- AND THIS is not working -> It comes from the other Button
   end startTimer





Here is the code for Button Stop:

Code: Select all

on mouseUp pMouseButton
   
   global stopcountdown
  put 1 into stopcountdown  -- THIS is to Stop the if condition, but it does not work. Why?
   
end mouseUp
So timer is working every 4 Seconds, fine. But I can not stop it with the Stop Button... And the question is... Why?

Stef

Re: Repeat Timer

Posted: Tue May 22, 2018 8:06 pm
by FourthWorld
The global is not declared in the startTimer handler.

Re: Repeat Timer

Posted: Tue May 22, 2018 8:14 pm
by ace16vitamine
Declared, but the same issue. Stop Button is not working

Code: Select all

on startTimer
   global stopcountdown
   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 4 second
   
   if stopcountdown is  1 then exit to top
   
end startTimer

Re: Repeat Timer

Posted: Tue May 22, 2018 8:28 pm
by Klaus
Hi ace,

the order of commands in not correct, check this:

Code: Select all

on startTimer
   global stopcountdown

   ## !! Here! :-)
   if stopcountdown = 1 then exit to top
   ## !!

   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 4 second     
end startTimer
Best

Klaus

Re: Repeat Timer

Posted: Tue May 22, 2018 8:30 pm
by ace16vitamine
aaaah you are right!

Thank you Klaus, it is working now

Re: Repeat Timer

Posted: Wed May 23, 2018 4:14 pm
by ace16vitamine
But now it is funny. I am trying so send every 216000 my email. This is working. But I receive this email 5 times, sometimes 1 minute later.

Code: Select all

on startTimer
   global stopcountdown
   
   if stopcountdown = 1 then exit startTimer -- to top

   
   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 216000 seconds
      
  dosendmail
   
end startTimer


But why? It seems that

Code: Select all

on mouseUp pMouseButton
   
   global stopcountdown
   put 1 into stopcountdown
 end mouseUp
does not stop the loop.

Re: Repeat Timer

Posted: Wed May 23, 2018 5:50 pm
by dunbarx
Hi.

Without seeing the code in handler "doSendMail", it is hard to tell.

One generic solution is to change:

Code: Select all

if stopcountdown <= 1 then exit startTimer -- to top
This often catches situations where the "1" is skipped, ignored or irrelevant. :wink:

Craig

Re: Repeat Timer

Posted: Wed May 23, 2018 8:40 pm
by jacque
Sometimes the code runs so fast that you will get a backup of pending messages in the queue. To avoid that you should check if the message is already in the queue before sending another one. (This showed up in another thread here recently.)

Code: Select all

on startTimer
   global stopcountdown
   
   if stopcountdown = 1 then exit startTimer -- to top

   
   beep 2 --here is where you do your own stuff
  if "startTimer" is not in the pendingMessages then send "startTimer" to me in 216000 seconds -- CHANGED
      
  dosendmail
   
end startTimer