从正在调试的代码附加到调试器事件?

时间:2011-03-17 20:50:55

标签: visual-studio visual-studio-2010 events debugging

在我可以在代码中订阅的调试中,Visual Studio是否会引发任何事件?具体来说,我想知道调试器何时被分离或停止。感谢

2 个答案:

答案 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符号。