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.
c # hang console-application console.writeline
Gordon Roxburgh Jun 27 '11 at 20:45 2011-06-27 20:45
source share