nooogle / CDS.CSharpScripting

Drop-in user control for C# editing with intellisense and compilation. Script compilation and execution classes for GUI and console applications.
MIT License
9 stars 3 forks source link

Can't cancel stuck script #25

Closed nooogle closed 3 years ago

nooogle commented 3 years ago

If script gets stuck, say in an infinite loop, there's no way to halt it.

nooogle commented 3 years ago

Think we need to use cancellation tokens when running the actual script.

nooogle commented 3 years ago

Yikes! So CSharpScript.RunAsync doesn't actually return until the script has completed! Using a cancellation token doesn't cancel a long running script.

From digging on Google it seems that no one can do this, at least not easily. The Visual Studio interactive C# script panel does allow a Reset, but it looks like the script runs in a separate process which is how it can be killed off; it also means a script run like this can't communicate with its host directly which is what I need from this library.

Rats.

It might be possible to run a script in a wrapper task, and then kill the task??

nooogle commented 3 years ago

At the very least - add a demo form test to show how the global variables object can be used to provide a signal (even a simple bool QuitScript property would suffice)

nooogle commented 3 years ago

Added AsyncRun to the script runner and AsyncGo to to the easy script utility; these just wrap their respective synchronous method in a task, but do allow scripts to run asynchronously. To cancel/abort a long running a script the host must use a globals object to signal a boolean or similar variable that the script can monitor. Demos added for both methods in V1.0.10.