What happens when you Start a Chart that was Stopped earlier, but has not fully stopped?

What happens when a Start Chart command is used on a chart that has been stopped, but has not fully stopped yet?

I have a chart (MyChartA) that initializes and device, then monitors some inputs to raise alarms if those inputs are not within prescribed ranges. It continues monitoring these values until an external chart stops it. This chart goes something like this:

*powerLevel = someNumber;
*deviceSwitch = 1;

while ( true ) {
    DelaySec(2);
    if ( *powerFeedback < lowerLimit ) then
        PowerAlarm = 101;
    elseif (*powerFeedback :L> upperLimit ) then
        PowerAlarm = 102;
    endif
wend

It is started from another chart (MyProcessChart). This other chart goes something like this:

// Do some initialization

Start Chart MyChartA

// Wait for process to complete.

Stop Chart MyChartA

I observer that if I run MyProcessChart back-to-back, that sometimes MyChartA does not start. If I set breakpoints and step through the start and stop chart statements, MyChartA always starts as it should. However if I run without breakpoints, I can look at the status of MyChartA when MyProcessChart is in the Wait for process to complete. logic. The first time I tun MyProcessChart, MyChartA is running, but the second time MyProcessChart gets to that portion of the logic, MyChartA is not running.

My guess is that if MyChartA is in the DelaySec() block when it is stopped, and the Start MyChartA the second running of MyProcessChart gets executed before the 2 second delay is up, then MyChartA does not get properly started.

The problem goes away if I explicitly wait for MyChartA to stop:

// Do some initialization

Start Chart MyChartA

// Wait for process to complete.

Stop Chart MyChartA

// Wait for MyChartA to stop

while IsRunning(MyChartA)
     DelayMSec(1);
wend

Is that correct, or is there something else going on?

Your observations are correct. I just recently experienced this behavior in one of my customers strategies. If the chart is still running when StartChart is called, the StartChart will be ignored, even if there is a pending StopChart.

The StopChart command does not execute until the chart requested to stop gets the time slice. So if the stopping chart is “executing” a delay instruction, the scheduler will not give that chart the time slice until the delay has expired.

As an application note, I find it is best to design strategies that don’t depend on stopping charts, but rather have flags in the chart to determine if the code on the chart should be executing or not. This also prevents charts from stopping in a random spot in the code that then would need cleaned up.

2 Likes