Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminate Roslyn script early in C#

Tags:

c#

.net

roslyn

I'm loading and invoking a csx file something like this:

BaseScript = new ScriptBase(); // My own type
var session = engine.CreateSession(BaseScript, typeof(ScriptBase));
BaseScript.Session = session;
//...
var script = File.ReadAllText(file);
BaseScript.Session.Execute(script);

This script is meant to run indefinitely, and basically has a while (true); in there. Is there a way for me to stop it prematurely?

Also, I'm thinking about moving to Mono.Csharp, would that allow me to do this? Maybe it's a better choice overall, thoughts?

like image 361
Drew DeVault Avatar asked Jan 29 '26 20:01

Drew DeVault


1 Answers

So the answer is this: Roslyn doesn't interpret C#, it compiles it. When you call Execute(string), it actually executes it right then and there. I put the call in a new thread and Abort()ed it when I needed to, which isn't especially elegant, but I'm not sure what else to do.

like image 100
Drew DeVault Avatar answered Feb 01 '26 10:02

Drew DeVault



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!