# Cancel flag in scheduled events

**URL:** <https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444>\
**Category:** Support\
**Created:** [May 17, 2016, 3:58pm UTC](https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444 "2016-05-17T15:58:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 17, 2016, 3:58pm UTC](https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444/1 "2016-05-17T15:58:59Z")

</div>

Hi Chris,

I am implementing a scheduled events to send regular pulses between Mworks and OE. Currently the cancel flag for the events is set to 1 at the exist system state of the task. This works for most of the times, however, sometimes it does not set to 1 when I stop the program and this causes Server to crash. I was wondering if there is a way to get around this problem.

Thanks,  
Eghbal

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 18, 2016, 2:11pm UTC](https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444/2 "2016-05-18T14:11:47Z")

</div>

Hi Eghbal,

As I’ve [noted before](https://mworks.discourse.group/discussions/questions/643#comment_39138255), the fact that scheduled actions don’t stop automatically when the experiment stops is a bug that needs to be fixed. I will try to get that fix in to the nightly build soon.

As a more general-purpose solution to this sort of problem, we could provide a mechanism to register actions that _always_ execute when an experiment terminates, irrespective of _why_ it terminates (i.e. normal termination or explicit stop). This is a feature that has been [on my to-do list](https://mworks.lighthouseapp.com/projects/23610/tickets/97) for a while, but I haven’t gotten to it yet.

In the meantime, the attached example demonstrates a method you can use to ensure that scheduled actions are canceled when the experiment stops. The key part is the `run_python_string` action at the end of the scheduled action list, which queries the system variable `#state_system_mode` (normally not accessible to experiments) and cancels the actions if the experiment is stopping or stopped. To use this in your own experiments, you’ll need to change the name `cancel` in the Python code to whatever your cancellation variable is called.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 18, 2016, 2:11pm UTC](https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444/3 "2016-05-18T14:11:47Z")

</div>

Attachment: [scheduled\_action\_cancel\_failsafe.xml](https://mworks.discourse.group/uploads/short-url/gjL4k3LoyQe5I9uayUILTpXpgw7.xml) (1.73 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 20, 2016, 2:14pm UTC](https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444/4 "2016-05-20T14:14:32Z")

</div>

Hi Eghbal,

> the fact that scheduled actions don’t stop automatically when the experiment stops is a bug

This is fixed in the current nightly build.

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 10:02pm UTC](https://mworks.discourse.group/t/cancel-flag-in-scheduled-events/444/5 "2022-07-19T22:02:25Z")

</div>


