timer action trigger question(desktop application timer)

I have two processes need to be executed repeatedly in my desktop application. Something likes following,

Process_A -> time_interval_1(e.g. 5 sec) -> Process_B —> time_interval_2(e.g. 10 sec) —> Process_A -> time_interval_1(e.g. 5 sec) -> Process_B …

I tried to set a timer for time_interval_1 and in the action of that timer, I use following pseudo code

execute Process_A
xojo.Core.Timer.CallLater(time_interval_2, AddressOf Process_B)

The problem is the timer will not wait for the CallLater() function to finish up and execute next round.
Does xojo allow a timer to trigger another timer inside itself and wait for the inside one finishes up?
If not, what’s the property way fulfill this scenario?

How about using brute force like app.sleepcurrentthread(your time interval) between process a and b?

Why do you try to use CallLater when you can simply change the period at the beginning of the Action event ?

[code]me.Period = 5000
ProcessA

me.period = 2000
ProcessB

me.Period = 10000
ProcessA[/code]

Nope, tried the code, it changes the whole timer period instead of adding time interval between Process_A and Process_B. My test is trying to print out something thru serial port, so instead of seeing the text of “process a is running” after 10 sec seeing “process b is running”, I saw both text strings printed out without any time interval between those.

[quote=291491:@Michel Bujardet]Why do you try to use CallLater when you can simply change the period at the beginning of the Action event ?

[code]me.Period = 5000
ProcessA

me.period = 2000
ProcessB

me.Period = 10000
ProcessA[/code][/quote]
I also tried your code, same result, doesn’t work. And I don’t understand the code either, you set the period of the timer, but in my test, the timer never resets itself after changing the period. like I mentioned above, I never see the serial port print out two strings with the time interval between them, they always show the second one right after the first one, without any time interval between.

I am sorry, but changing the period in the action event of a timer or a Xojo.Core.Timer does work.

Now, since I don’t know what you are after, good luck.

The timer used by the CallLater method is not the same timer that generate the action event so if you set the timer in ModeMultiple the timer auto restart and generate an action event when the timer period expires.

You can use at the end of processA:

xojo.Core.Timer.CallLater(time_interval_2, AddressOf Process_B)

and at the end of processB:

xojo.Core.Timer.CallLater(time_interval_1, AddressOf Process_A)

[quote=291529:@Maurizio Rossi]The timer used by the CallLater method is not the same timer that generate the action event so if you set the timer in ModeMultiple the timer auto restart and generate an action event when the timer period expires.

You can use at the end of processA:

xojo.Core.Timer.CallLater(time_interval_2, AddressOf Process_B)

and at the end of processB:

xojo.Core.Timer.CallLater(time_interval_1, AddressOf Process_A)

So do you mean I don’t need the timer, instead, just manually make the time loop? Where should I call the processA and B? in an open event?

I currently call process A and B in the action event of the timer. That’s why I have the question above. Correct me if I was wrong.

You could just have two timer, one for each process.
Than set mode to single for the other timer when one process is done.

Hi Bo,
if your processes must start when the application open you can start processA in the open event of the main window.

The cleanest way is to use two timers as Christian suggested: simply set both timers in ModeSingle.
From your description your application requires a delay after completing one process before starting the other one, so use the timers to have a single action event (ModeSingle) and when a process is done simply restart the timer related to the other process.

P.S.
From the description of your program behaviour I suspect that there is something strange in your code.
Post here if you need more assistance.

Regards.

[quote=291585:@Christian Schmitz]You could just have two timer, one for each process.
Than set mode to single for the other timer when one process is done.[/quote]

[quote=291588:@Maurizio Rossi]Hi Bo,
if your processes must start when the application open you can start processA in the open event of the main window.

The cleanest way is to use two timers as Christian suggested: simply set both timers in ModeSingle.
From your description your application requires a delay after completing one process before starting the other one, so use the timers to have a single action event (ModeSingle) and when a process is done simply restart the timer related to the other process.

P.S.
From the description of your program behaviour I suspect that there is something strange in your code.
Post here if you need more assistance.

Regards.[/quote]
Thank you for your reply.

I set up two timers now. Timer1 for 5 sec interval, Timer2 for 10 sec.
In Timer1 action event,

run prossess A
Timer2.Mode = Timer.ModeSingle

In Timer2 action event

run process B

My running result still has the same problem. Since Timer1 is only 5 sec and Timer2 is 10 sec, once process A finished, before process B is running, the process A will still be executed twice, which means Timer1 is continue running without waiting for Timer2 event action finishes. Because it’s a two timers system, this behavior makes sense to me, but it’s not what I want.

More confusing situation is I made another new experiment project with only one window and 1 textarea and two timers. This time the timer2 is not even been triggered. I put the sample project file at following,
link text
the code is really simple,
Window1 open action

  Timer1.Enabled = True
  Timer2.Enabled = True
  
  Timer1.Mode = Timer.ModeMultiple

Timer1 action

  TextArea1.AppendText ("Timer1 event starts..." + EndOfLine)
  
  TextArea1.AppendText("process A is running..." + EndOfLine)
  
  TextArea1.AppendText("process A finished!" + EndOfLine)
  
  Timer2.Mode = Timer.ModeSingle

Timer2 action

  TextArea1.AppendText( "timer2 event starts..." + EndOfLine)
  
  TextArea1.AppendText("process B starts..." + EndOfLine)
  
  TextArea1.AppendText("process B finished!" + EndOfLine + EndOfLine + EndOfLine)
  

I don’t know how to explain this, but I never see the text string “process B starts…” ect., only the process A is running. very strange and I’m pulling hair…

Visibly you are trying to start a process when another is finished. Or so I sort of understand from you laborious description.

Instead of using timers, generate some end of process event in your processes, so you can start the following one.

Using arbitrary timers is a recipe for issues if processe don’t always have fixed durations.

This is a familiar theme of industrial processes.

Heck, only home washing machines are still using timers.

Hi Bo,
your program needs some modification (the text format version i.e. twotimers.xojo_project).

Replace the instructions in the window.open event with:

Timer1.Mode = Timer.Modesingle

In the Timer2.action event append as last instruction:

timer1.Mode = timer.ModeSingle

Using the inspector set for both timers mode=off.

[quote=291727:@Michel Bujardet]Visibly you are trying to start a process when another is finished. Or so I sort of understand from you laborious description.

Instead of using timers, generate some end of process event in your processes, so you can start the following one.

Using arbitrary timers is a recipe for issues if processe don’t always have fixed durations.

This is a familiar theme of industrial processes.

Heck, only home washing machines are still using timers.[/quote]
Yep, you can’t believe some industrial processes are still using Atari console boards. >_-

[quote=291740:@Maurizio Rossi]Hi Bo,
your program needs some modification (the text format version i.e. twotimers.xojo_project).

Replace the instructions in the window.open event with:

Timer1.Mode = Timer.Modesingle

In the Timer2.action event append as last instruction:

timer1.Mode = timer.ModeSingle

Using the inspector set for both timers mode=off.[/quote]
Yep, it works now. Thanks a lot! Definitely learned a lesson and it’s on time too, save most of the hair on my head. :slight_smile: