Debug.WriteLine锁

编程入门 行业动态 更新时间:2024-10-23 01:40:13
本文介绍了Debug.WriteLine锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的程序经常与死锁停止。当我做一个突破一切,看看我看到三个线程都停留在我们的记录功能的线程:

公共类记录 {公共静态无效WriteClientLog(LogLevel的日志级别字符串消息) {#如果DEBUG System.Diagnostics.Debug.WriteLine(的String.Format( {0} {1},DateTime.Now.ToString(HH:MM:SS),消息)); // LOCK #ENDIF //...Log4net记录} }

如果我让程序继续线程都还停留在那一行。

我看不出这个可以锁定。 Debug类,串类功放和; DateTime类似乎是线程安全的。

错误消失,当我删除#如果调试系统... #ENDIF 代码,但我很好奇,为什么这种行为发生。

主题之一:

公共无效CleanCache() { Logging.WriteClientLog(LogLevel.Debug,开始清理缓存。); //卡住}

主题二:

私人无效AliveThread() { Logging.WriteClientLog(LogLevel.Debug,检查连接); //卡住}

解决方案

的的Debug.WriteLine 写入日志消息连接到的听众集合。

你的一个跟踪听众必须在内部有一个锁,导致死锁。检查你的听众的代码,因为它是最有可能的罪魁祸首。

My program frequently stops with a deadlock. When I do a break-all and look at the threads I see that three threads are stuck in our logging function:

public class Logging { public static void WriteClientLog(LogLevel logLevel, string message) { #if DEBUG System.Diagnostics.Debug.WriteLine(String.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), message)); //LOCK #endif //...Log4net logging } }

If I let the program continue the threads are still stuck on that line.

I can't see where this can lock. The debug class, string class & datetime class seem to be thread safe.

The error goes away when I remove the #if DEBUG System... #endif code but I'm curious why this behavior happens.

Thread one:

public void CleanCache() { Logging.WriteClientLog(LogLevel.Debug, "Start clean cache.");//Stuck }

Thread two:

private void AliveThread() { Logging.WriteClientLog(LogLevel.Debug, "Check connection");//Stuck }

解决方案

Debug.WriteLine writes logging messages to the attached Trace Listeners attached to the Listeners collection.

One of your trace listeners must have a lock internally which is causing a deadlock. Check your listener code, as it's most likely the culprit.

更多推荐

Debug.WriteLine锁

本文发布于:2023-05-28 12:30:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/320927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Debug   WriteLine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!