Killing guest process after query finishes
I am aware of the "Always Preserve Query Process" and "Always use Fresh Process per Execution" settings. But how can I ensure that the process is terminated without manual intervention (Query > Kill process) once the query finishes? I don't need a setting; if I can add a last statement to my code, that's also fine with me.
Comments
-
There are two ways to do this. The first is with the following code:
Util.NewProcess = true;
This tells LINQPad to use a fresh process next time the script runs, similar to the setting "Always use Fresh Process per Execution".
The second (which is I think what you want) is to execute the following:
Environment.Exit (0);
This immediately ends the script - LINQPad will show "Script ended with exit code 0" in the status bar.
-
Is there a way to achieve a similar effect in LINQPad 5?
Environment.Exit(0)results in a message overlay saying 'Query ended unexpectedly.', and from LINQPad's point of view the query process seems to be still loaded. That is, subsequent SHIFT+F5 results in a status bar message saying 'Query Process Unloaded'.On the other hand, process resources like memory, SQL connections and so on do get released by
Environment.Exit(0), meaning that most of the desired effect has been achieved. Printing 'DONE' to the result window (suitably styled viaUtil.WithStyle()) gives a clear indication that the script finished successfully and that the warning about the script having ended unexpectedly can be ignored.So I guess I could live with the strange warning and start to reinterpret it as 'all resources released, no sweat'. ;-)
However, if there were some trick to achieve this cleanly then I would very much appreciate it. I've toyed with the idea of sending the SHIFT+F5 keystroke to editor window at the end of the script but I have no idea how to get the right window handle reliably. Is there some way to issue SHIFT+F5 via LINQPad APIs, perhaps with a bit of reflection to deal with 'poor visibility'?
