DEBUGGER: STOP SCRIPT BUT KEEP THE MAIN APPLICATION RUNNING

I have a main application that allows running scripts. The scripts are edited and debugged with Scripter from a separate process. Everything works fine. I can start debugging, pause, step, etc.
Now the problem. A user can write a script like this:
public void UserScript()
{
while(true)
{
MessageBox.Show("endless loop")
}
}
When this script is started with the debugger, there is no way to stop it. When you press Stop with StopDebuggerAsync, the debugging stops but the script will run forever in the main application. This is expected.
My question is, is there any trick to solve this situation?
Currently, all I can do is to kill the main process.
Maybe there's a way to exit just from MyCodeModule code? I think it's not possible.
In VS, I can break the execution and move the execution point out of the endless while block and then Continue. But ScriptDebugger only has GetExecutionPositionAsync(), there's no way to change it.
Any ideas are welcome.
Thank you
Hi,
there is no good way to stop the script unless you run it in the separate thread and pass cancellation flag from the main thread (you may also use async task and pass Cancellation token)
Here is code snippet showing this approach:
volatile bool cancel;
void StartThreadButton_Click()
{
cancel = false;
new Thread(ThreadProc).Start();
}
void CancelButton_Click()
{
cancel = true;
}
void ThreadProc()
{
while (!cancel)
{
Console.WriteLine("Test");
}
}
We will investigate feasibility of supporting moving execution line to the next statement in our debugger.
regards,
Andrew
Thank you.
I was aware of checking the cancellation flag/token. But in your example, the ThreadProc() represents a user script. And we have no control about it. We have no way to force the user to insert cancellation checks in his code.
Before we needed the debugging functionality, we simply injected such checks into the compiled asembly with Mono.Cecil. This has been working perfectly. But this approach is not possible with an assembly with debug info. The injection breaks the validity of PDB and debugging is no longer possible.
So it would be great if there was some way to move the execution line. Even a limited one, e.g. move to the end of the current method (which is represented by the current stack frame?).
Hi,
We can support SetNextStatement (it will require some efforts), however it does not look like it will help with exiting from end-less loop.
I tried the following code in Visual Studio, and it does not seem to be able to move execution line out from the loop - please let me know if you're getting different results.
class Program
{
public static void UserScript()
{
while (true)
{
Console.WriteLine("endless loop");
Console.WriteLine("endless loop");
Console.WriteLine("endless loop");
}
Console.WriteLine("exit");
}
static void Main(string[] args)
{
UserScript();
}
}
regards,
Andrew
Andrew,
That's because the line:
Console.Writeline("exit");
is unreachable and it's probably not compiled at all. Just change the condition to:
int i=0;
while (i < 5)
and you'll be able to move the execution point. I used my original script just as a very simple example. In reality, the scripts are more complex, but they may lock the main application, which happens more often then we like :)
Hi,
We will investigate how to support this feature and I will let you know as soon as we have some results.
regards,
Andrew
Hi,
We have added Set Next Statement, available when execution is stopped from popup menu and moves execution line to the position under the caret.
Please be aware that if execution stops somewhere in system code, you might need to do a step over/step out for it to get back to the script code before Set Next Statement can be successfully executed.
Please try uploaded demo and let me know if it works well for you:
http://www.alternetsoft.com/forum/SNSDemo.7z
regards,
Andrew
Andrew,
I've performed some tests and it seems to be working perfectly. The only issue was that I couldn't set the next statement to the closing } of a C# method. But that's exactly how it works also in Visual Studio. I wasn't aware of this limitation. On the other hand, when I tried a VB project, I could easily move to the last End Sub or End Function line. That's great because 90% of our clients use VB for our macros.
So I'm very satisfied with this solution.
Hi,
Thank you for letting me know that it all works good, we will include this feature in the next public release.

regards,
Andrew