Hello Maladie.
I suppose that you launch the process by a statment
::testing(10)
somewhere in the code. Your process does not last: the switch is not a durative action: it executes in the instant where the process is launched and the the process died. So the gloval variable $cue
is tested only once, when the process is launched. You can launch periodically the process with a loop. But if you really want to react each time $cue
is updated, the whenever
is teh right control structure.
You may put a call to ::testing
in the whenever
:
whenever ($cue) @exclusive {
status “variable received”
status $cue
::testing($cue)
}
in this way, each time $cue
is updated, the process ::testing
is called.
You may also include a whenever
in the process ::testing
:
@proc_def ::testing($var1) {
whenever ($cue == $cue) {
switch ($cue) {
case 1: $result := 2
case 2: $result := 4
}
}
This code call for several comments. When the process is launched, the whenever
is launched as a sub-process of the process. This sub-process live forever (it must be explictly killed or scoped with a during
clause) as for any whenever
and will execute the switch each time $cue
is updated somewhere
The second remark is that we use the condition ($cue == $cue)
because we want to trigger the whenvevr body even if $cue
is set to zero (or false): the whenever executes the body if the variable is updated and the condition evaluate sto ture. The expression ($x == $x)
is teh simplest expression which evaluates to true each time the variable $x
is updated.
The third comment is that we explicitly use the variable $cue
in the condition of the whenever
. The following code
@proc_def ::bad_testing($var1) {
whenever ($var1 == $var1) {
switch ($cue) {
case 1: $result := 2
case 2: $result := 4
}
}
::testing($cue)
does not do the intended job for the following reason: the whenever
in ::bad_testing
is watchu-ing the local variable $var1
(a process’s parameter is indeed a local variable of the process) not the variable given as argument of the call.