I am currently trying to run some code when the debugger detaches from a process. It is easy to find out if a debugger is attached:
System.Diagnostics.Debugger.IsAttached;
My question is if there is a way (preferable one that works for .NET, Windows Phone, WinRT) to get an event when the debugger gets detached (mostly when the application is killed).
Worst case I can find the debugger Process in .NET and subscribe to the Exit event, but that won't work in Windows Phone and WinRT.
Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration. Find and check the box Run in Terminal.
We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension. In this image, click on the Install button to install the C/C++ extension.
C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS.
Probably the simplest way is to have a thread watching the value. Something like:
public class DebugEventArgs : EventArgs {
public bool Attached { get; set; }
}
class Watcher {
public event EventHandler<DebugEventArgs> DebuggerChanged;
public Watcher() {
new Thread(() => {
while (true) {
var last = System.Diagnostics.Debugger.IsAttached;
while (last == System.Diagnostics.Debugger.IsAttached) {
Thread.Sleep(250);
}
OnDebuggerChanged();
}
}){IsBackground = true}.Start();
}
protected void OnDebuggerChanged() {
var handler = DebuggerChanged;
if (handler != null) handler(this, new DebugEventArgs { Attached = System.Diagnostics.Debugger.IsAttached });
}
}
(written but not compiled)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With