从 VS 运行代码后,我们有一个包含多个项目的解决方案,通常从 Debug.Writeline 语句中看到的输出不再出现。我提到多个项目是因为其中一个项目的输出继续出现。但是,另一个项目始终停止显示语句的输出。
它开始让我发疯。我应该提到这也发生在该项目的第二位开发人员身上。有没有人见过这个,或者有什么想法?
最佳答案
被这个问题折磨了多年,终于在这个 Stack Overflow 问题中找到了原因和解决方法:vs2010 Debug.WriteLine stops working
似乎 Visual Studio 的 debug.writeline 处理无法处理每个正确使用多个线程的多个进程。最终这 2 个进程将使处理输出的 Visual Studio 部分死锁,导致它停止工作。
解决方案是将您对 debug.writeline 的调用包装在一个类中,该类使用命名的互斥锁在进程间同步。这可以防止多个进程同时写入调试,很好地避免了整个死锁问题。
包装器:
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 的人:
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
关于visual-studio-2010 - 为什么 Debug.Writeline 停止为解决方案中的某些项目工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5992094/