在我可以在代码中订阅的调试中,Visual Studio是否会引发任何事件?具体来说,我想知道调试器何时被分离或停止。感谢
答案 0 :(得分:2)
在单独的线程中,您可以检查Debugger.IsAttached
方法并相应地生成事件。
public class DebuggerEvents {
public delegate void DebuggingStatusChanged(EventArgs e);
public event DebuggingStatusChanged DebuggerAttached;
public event DebuggingStatusChanged DebuggerUnattached;
private readonly Thread workerThread;
public DebuggerEvents() {
var threadStart = new ThreadStart(BackgoundWorker);
workerThread = new Thread(threadStart);
workerThread.Start();
}
private void BackgoundWorker() {
bool currentStateflag = Debugger.IsAttached;
while (true) {
if (Debugger.IsAttached && !currentStateflag){
DebuggerAttached(new EventArgs());
currentStateflag = true;
}
if (!Debugger.IsAttached && currentStateflag){
DebuggerUnattached(new EventArgs());
currentStateflag = false;
}
Thread.Sleep(100);
}
}
}
答案 1 :(得分:1)
试试这个:
System.Diagnostics.Debugger.IsAttached
还要查找条件编译符号并注意DEBUG符号。