Rarely hangs in a multithreaded C # console application when using Console.Writeline () or Console.Write ()

I wrote a console application that uses console.write and console.writeline to provide some logs. An application is a server application that uses asynchronous beginacceptconnection () and beginread () (Sockets) for communication. Sometimes I get messages that it hangs, and from the limited debugging that I can do, I see that the problem is related to Console.Writeline () or Console.write ().

Being multi-threaded, I tried to block the logging class, so only one thread can log a message at once ... When I caught a hang, all I get is blocking threads on a lock and VS reports that the control passed to Console.Write, and he waits for him to return ... he never does that.

A few days ago I got another failure report, but this time during boot ... where the asynchronous connection was not connected (the main thread starts the thread to load), and a picture was sent to me ..... see below. (I added start and end critical sections to prevent this, and it is not)

// Logging Class public class Logging { // Lock to make the logging class thread safe. static readonly object _locker = new object(); public delegate void msgHandlerWriteLineDelegate(string msg, Color col); public static event msgHandlerWriteLineDelegate themsgHandlerWriteLineDelegate; public delegate void msgHandlerWriteDelegate(string msg, Color col); public static event msgHandlerWriteDelegate themsgHandlerWriteDelegate; public static void Write(string a, Color Col) { if (themsgHandlerWriteDelegate != null) { lock (_locker) { themsgHandlerWriteDelegate(a, Col); } } } public static void Write(string a) { if (themsgHandlerWriteDelegate != null) { lock (_locker) { themsgHandlerWriteDelegate(a, Color.Black); } } } public static void WriteLine(string a, Color Col) { if (themsgHandlerWriteLineDelegate != null) { lock (_locker) { themsgHandlerWriteLineDelegate(a, Col); } } } public static void WriteLine(string a) { if (themsgHandlerWriteLineDelegate != null) { lock (_locker) { themsgHandlerWriteLineDelegate(a, Color.Black); } } } // Console Methods That implement the delegates in my logging class. public static void ConsoleWriteLine(string message, Color Col) { try { if (Col == Color.Black) { Console.ForegroundColor = ConsoleColor.Gray; } else { Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Col.Name); } Thread.BeginCriticalRegion(); Console.WriteLine(message); Thread.EndCriticalRegion(); Console.ForegroundColor = ConsoleColor.Gray; } catch (ThreadAbortException ex) { Console.WriteLine("ThreadAbortException : " + ex.Message); } catch (Exception ex) { Console.WriteLine("Exception : " + ex.Message); } } public static void ConsoleWrite(string message, Color Col) { try { if (Col == Color.Black) { Console.ForegroundColor = ConsoleColor.Gray; } else { Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Col.Name); } Thread.BeginCriticalRegion(); Console.Write(message);//**THIS IS WHERE IS HANGS...IT NEVER RETURNS ** Thread.EndCriticalRegion(); Console.ForegroundColor = ConsoleColor.Gray; } catch (ThreadAbortException ex) { Console.WriteLine("ThreadAbortException : " + ex.Message); } catch (Exception ex) { Console.WriteLine("Exception : " + ex.Message); } } public static void ConsoleUpdate(string message) { try { Thread.BeginCriticalRegion(); Console.WriteLine(message);//**THIS IS WHERE IS HANGS...IT NEVER RETURNS ** Thread.EndCriticalRegion(); } catch (ThreadAbortException ex) { Console.WriteLine("ThreadAbortException : " + ex.Message); } catch (Exception ex) { Console.WriteLine("Exception : " + ex.Message); } } // The main method...subscribes to delegates and spawns a thread to boot HW..main thread then exits. public static void Main() { Logging.themsgHandlerWriteDelegate += new Logging.msgHandlerWriteDelegate(ConsoleWrite); Logging.themsgHandlerWriteLineDelegate += new Logging.msgHandlerWriteLineDelegate(ConsoleWriteLine); Logging.themsgHandlerUpdateDelegate += new Logging.msgHandlerUpdateDelegate(ConsoleUpdate); } } public class ClassOnOtherThread { // In a different class running on a different thread the following line occasionly invokes the error: private void BootHw(string Resource, string Resource2) { Logging.Write("\t\t["); } } 

My MSDN reading suggests that Console.WriteLine and Console.Write are thread safe, and so I actually don't need a lock ... I also can't believe that the Microsoft code is wrong (;-) and therefore I assume this is an interaction that does my code that creates an error.

Now my question is: should I do anything to prevent Console.WriteLine and Console.Write from interrupting? ... this is my assumption that something is interrupting it ... but I really don't know what !!

I really liked any help.

Hello,

Gordon.

+8
c # hang console-application console.writeline
Jun 27 '11 at 20:45
source share
5 answers

I had the same problem.

I used console.readkey() in the main thread to prevent the application from closing in debug mode.

After I replaced it with an infinite loop, my problem was resolved.

+8
Sep 11 '12 at 13:52
source share
β€” -

Consol.Writeline() is thread safe. So

I tried to block the logging class

This should not be necessary and may well be the cause of the dead ends. Without any code samples, it is impossible to say if / how / what.

and therefore I assume that this is an interaction that makes my code that creates an error.

Pretty likely.

I think something is interrupting him.

No.

+2
Jun 27 '11 at 20:50
source share

You must solve your problem by removing the locks around the log. Logging is done through Console.WriteLine , which is synchronized (and thread safe). You may have come to a standstill through your own locking mechanism (although I can’t verify without seeing the code).

+2
Jun 27 '11 at 20:52
source share

This is a bit long snapshot, but I'm wondering if you are calling Console.WriteLine with objects, the ToString () method takes a lock. If so, you may be in a deadlock regarding locking made inside Console.WriteLine.

I once posted this bug report on Microsoft Connect, although unfortunately they refused to fix it.

+2
Jun 27 '11 at 22:17
source share

I assume that your application is started by another process that redirects stderr and stdout. If your observer process uses ReadToEnd () for both threads in the same thread, you can block.

Another variant of the deadlock is sending the child process via stdin, which, in turn, starts another process with the console, which waits for input indefinitely. This happened once with wmic.exe, which blocks when stdin is redirected.

If you played in your journal class, I suspect that you have modified the underlying Stream of Console.Out with your own. Please place at least a stop call where your application hangs, so we have something to analyze. There are many ways to shoot in the leg if you replace the console stream with your own.

Regards, Alois Kraus

+1
Jun 27 '11 at 20:53
source share



All Articles