2

I have this problem that has been giving me grief for most of the day and then some.
Basically my Debug.WriteLine works for a bit, and then magically stops giving output. The debugger is still working because I can put a breakpoint on the line, and it will break, and I can step over the Debug.WriteLine and watch it not output.

Now I have checked everything I can think of. I have tried things from various posts such as

Debug.Writeline is Not printing anything

Debug.Writeline stops working

Visual Studio 2010 suddenly stops displaying Debug output

but nothing has worked. To clarify

  • I am using vs2010 pro sp1.
  • The projects are using the .net4 framework.
  • I have the Debug Constants Set.
  • The Debugger is still attached as I can set breakpoints and they are hit.
  • Both projects are set to build under "Any CPU" (I tried with both as X86 and it didnt help)
  • I am running on Windows 7 x64

And after following all the suggestions, I dont have a fix. None of those Posts above were actually answered.

So just how does one fix this ?

I thought I would create a seperate solution to reproduce this problem.

You can download my solution that reproduces the bug here https://drive.google.com/uc?export=download&confirm=&id=0B0SLafLeQj5GOGxORThMWWhYV28

So I made 2 windows apps, and set them to both start at the same time. In each app I start a thread, and in the Thread I do a Debug.WriteLine

It seems to get some sort of deadlock and stop writing the output. This appears to be a bug in Visual Studio, which I am highly surprised at. Must I believe that it is impossible to have 2 projects with threads doing Debug.WriteLine at the same time ?

Once again, I am in deperate need of a way to fix this and still get Debug Output. Oh, and I have also tried the option of redirecting output to the immediate window with no success either.

Community
  • 1
  • 1
Steed
  • 287
  • 3
  • 15

2 Answers2

5

There seems to be a bug in visual studio. I used .net reflector to dig into the CLR and discovered that the locking it does only appears to work on a per process level. So the DefaultTraceListener appears to work fine with Multithreading when you only have 1 project running, however as soon as you have more than one project running it creates some kind of deadlock and stops working.

I solved this by creating a wrapper for the Debug.WriteLine where I then implemented a Global Mutex as a lock around the writes. The Mutex is a wrapper for a win32 mutex and so works across process level on the system.

See my code here that I put inside a class lib that I reference in both of the projects and use instead of the normal Debug.WriteLine under System.Diagnostics:

public class Debug
{
    #if DEBUG
        private static readonly Mutex DebugMutex =new Mutex(false,@"Global\DebugMutex");
    #endif

    [Conditional("DEBUG")]
    public static void WriteLine(string message)
    {
        DebugMutex.WaitOne();
        System.Diagnostics.Debug.WriteLine(message);
        DebugMutex.ReleaseMutex();
    }

    [Conditional("DEBUG")]
    public static void WriteLine(string message, string category)
    {
        DebugMutex.WaitOne();
        System.Diagnostics.Debug.WriteLine(message,category);
        DebugMutex.ReleaseMutex();
    }

}

Hope this helps !

Steed.

Steed
  • 287
  • 3
  • 15
5

Verify that the output window's context menu has "Program Output" checked. My debug statements were not tracing. This was unchecked even though I had not unchecked the option. Checking it fixed it for me.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636