How to wait until remote .NET debugger attached

这一生的挚爱 提交于 2019-11-28 03:39:04

You can use the System.Diagnostics.Debugger.IsAttached property to check if a debugger is attached to the process. This application will wait until a debugger has been attached:

using System;
using System.Diagnostics;
using System.Threading;

namespace DebugApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Waiting for debugger to attach");
            while (!Debugger.IsAttached)
            {
                Thread.Sleep(100);
            }
            Console.WriteLine("Debugger attached");
        }
    }
}

This sounds like exactly what you need:

Debugger.Launch();

http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx

"Launches and attaches a debugger to the process."

Steven Behnke

I don't know, since I've never tried it but I wonder if you could use System.Diagnostics.Debugger.Break() in order to have it hit a breakpoint and then wait for a debugger to attach. I assume a remote debugger would work, but I do not know for sure and currently do not have access to my home environment where I could easily mock it up and test my theory. There's an MSDN article talking about using it in an ASP.Net application so I imagine it would work.

Attaching remote debugger works exactly the same as using local debugger.

First, do the usual:

System.Diagnostics.Debugger.Launch();

You will see a prompt to chose debugger. At this point the execution is suspended, so so you can attach remote debugger and select "No" from the prompt.

Debug.Assert(true);

should also work I guess. By the way, I also face this proble at times and I do

MessageBox.Show() 

:P :P

Arron S

Set a timeout that gives you time to attach the debugger.

Thread.Sleep(30000);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!