Main Site Documentation

Check if is in debug attacched


#1

Hi to all,
on fez hydra is there a method to check if a debugger is attached?
For examples, I need to print some information only when I’m attached with some debugger console (Visual studio or MFDeploy).

Thanks


#2

debug.print output seems to go into the “void” if it’s not actively being debugged. I’ve seen people wrap IFDEFs around debug.print and compile debug or release versions, but I’ve never worried personally (just because I haven’t had any issue doesn’t mean its meant to work that way :))

edit: here’s a relevant thread http://www.tinyclr.com/forum/1/4128/


#3

Hi John,
Here is one way to do it:

First Check the Define Debug constant located here: Right Click over your Project >> Properties>> BUILD tab>> make sure configuration is set to Debug… and check the Define DEBUG constant if not already checked.



//create a public Method called DebugMsg() as follow:

        public static void DebugMsg(string msg)
        {
           #if DEBUG 
                  Debug.Print(msg);
           #endIf
        }


now click and hold Ctrl , Shift, F and choose the Quick Replace TAB…
in Look in: change that to Current Document.
in Find What: type: Debug.Print
in Replace with: type DebugMsg

hit Replace All…

after the above step make sure you fix this code if it changed:


           #if DEBUG 
                  Debug.Print(msg);
           #endIf

run your project in debug mode and what you messages being printed …
next change the configuration of your SOLUTION (if you have more than one Project) to release mode and your messages will no longer print.

now going forward you should be using DebugMsg() instead of Debug.Print… you can do the same for the other projects.

I hope this helps…
Cheers.


#4

Thanks for answers, but what I meant isn’t to compile in debug or release mode, but at runtime know if some debugger are attached or not.

Thanks


#5

in that case you can use this:


            if (System.Diagnostics.Debugger.IsAttached)
            {
              Debug.Print("debugger is present");
            }else
            {
                //the LED on your board will light up when running without the debugger
                for (int i = 0; i < 100; i++)
                {
                    PulseDebugLED();
                    Thread.Sleep(50);
                }
                Debug.Print("No debugger is present");//you won't see this because of no debugger.. can print to the LCD instead.
            }

Cheers,
Jay.


#6

Thanks Jay Jay, that is exactly what I was looking.

Thanks a lot.


#7

You are welcome :wink: