How to handle cancellation in C# scripts

If you want to be able to cancel running C# script during runtime with the kill switch, you need to place the possible cancellation stops in your script manually.

To start, every Chat message creation checks for an ongoing cancellation by default. Further, if you need to be able to cancel long running loops, use the following method if no Chat message will be created during the loop:

This allows you to kill a running C# script Task in a well-behaved and traceable way:

How to do cancellation checks in the C# script

Updated on February 24, 2025
So, how does that make you feel, now you've read this explanation?