为什么 Debug.Writeline 停止为解决方案中的某些项目工作? [英] Why does Debug.Writeline stop working for some projects in the solution?

查看:28
本文介绍了为什么 Debug.Writeline 停止为解决方案中的某些项目工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 VS 运行代码后,我们有一个包含多个项目的解决方案,通常从 Debug.Writeline 语句中看到的输出不再出现.我提到多个项目是因为其中一个项目的输出继续出现.但是,另一个项目始终停止显示语句的输出.

We have a solution with multiple projects after running the code from VS the output normally seen from Debug.Writeline statements just cease to appear. I mention the multiple projects because the output from one of the projects continues to appear. However, the other project consistently stops showing the output from the statements.

它开始让我发疯.我应该提到这也发生在该项目的第二位开发人员身上.有没有人见过这个,或者有什么想法?

It's starting to drive me crazy. I should mention this is also occurring for a second developer on the project. Anyone seen this before, or have any ideas?

推荐答案

在被这个问题折磨了多年之后,我终于在这个 Stack Overflow 问题中找到了原因和解决方案:vs2010 Debug.WriteLine 停止工作

After being tormented by this for years I finally found the cause and the solution in this Stack Overflow question: vs2010 Debug.WriteLine stops working

Visual Studio 对 debug.writeline 的处理似乎无法正确处理每个使用多个线程的多个进程.最终,这 2 个进程将使处理输出的 Visual Studio 部分死锁,导致其停止工作.

It seems that Visual Studio's handinlg of debug.writeline can't handle multiple processeses that each use multiple threads correctly. Eventually the 2 processes will deadlock the portion of visual studio that handles the output, causing it to stop working.

解决方案是将您对 debug.writeline 的调用包装在一个类中,该类使用命名的互斥锁在进程间同步.这可以防止多个进程同时写入调试,很好地避免了整个死锁问题.

The solution is to wrap your calls to debug.writeline in a class that synchronizes across processes using a named mutex. This prevents multiple processes from writing to debug at the same time, nicely side stepping the whole deadlock problem.

包装器:

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();
     }
}

或者对于那些使用 VB.NET 的人:

Or for those using VB.NET:

Imports System.Threading

Public Class Debug
#If DEBUG Then
  Private Shared ReadOnly DebugMutex As New Mutex(False, "Global\DebugMutex")
#End If

<Conditional("DEBUG")> _
Public Shared Sub WriteLine(message As String)
    DebugMutex.WaitOne()
    System.Diagnostics.Debug.WriteLine(message)
    DebugMutex.ReleaseMutex()
End Sub

<Conditional("DEBUG")> _
Public Shared Sub WriteLine(message As String, category As String)
    DebugMutex.WaitOne()
    System.Diagnostics.Debug.WriteLine(message, category)
    DebugMutex.ReleaseMutex()
End Sub
End Class

这篇关于为什么 Debug.Writeline 停止为解决方案中的某些项目工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆