Detatching the Debugger

I have an application that runs little scripts that can be edited at runtime. These scripts cannot be run in their own process and have to be executed from the main application. In order to debug these scripts i run the debugger in its own process, attaching it to the main application process. The problem I´m running in now is that when i stop debugging, the debugger closes the main application process. So my question is: Is there a way to detach (and close) the debugger without it killing the process it is attached to?


Did you try calling Debugger.StopDebuggingAsync?
For an example, see AlternetStudio sample, RemoteControl\RemoteControlMainForm.cs:

        protected override void OnClosed(EventArgs e)
            if (Debugger.IsStarted)
                Task.Run(async () => await Debugger.StopDebuggingAsync()).Wait();

You can try check this on DebugMyScript quick start application - when closing the debugger window the script process remains running.


thank you for the reply. The solution you provided works.
I had only tried:


before which just freezes and never finishes.

Glad to hear it works.

Regarding your question about the deadlock, please read this SO thread for an explanation.
The code I sent you in my last message uses the solution from the mentioned SO discussion:

Queue the continuation of the await to a different thread that is not blocked, e.g. use var data = Task.Run(GetDataAsync).Result , which will post the continuation to the sync context of a threadpool thread.