Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.WriteLine(""); gets stuck

Sometimes when I execute the above statement, the program freezes in a console application. If I break, I can't move to the next line. Do I need to reset a buffer or something?

It's a batch process application that displays messages to the screen. Has anyone experienced this and managed to resolve it. It seems to be a new thing. I'm using Visual Studio 2017 Prof. edition.

The function where WriteLine stalls is below. The value of sMessage is blank "".

static void Display(string sMessage, DisplayColours eColour = DisplayColours.White)
{
    if (eColour == DisplayColours.Yellow)
        Console.ForegroundColor = ConsoleColor.Yellow;
    if (eColour == DisplayColours.Blue)
        Console.ForegroundColor = ConsoleColor.Cyan;
    if (eColour == DisplayColours.Green)
        Console.ForegroundColor = ConsoleColor.Green;
    if (eColour == DisplayColours.Red)
        Console.ForegroundColor = ConsoleColor.Red;
    if (eColour == DisplayColours.Magenta)
        Console.ForegroundColor = ConsoleColor.Magenta;
    if (oFptr != null)
    {
        oFptr.WriteLine(sMessage);
        oFptr.Flush();
    }
    Console.WriteLine(sMessage);
    Console.ForegroundColor = ConsoleColor.White;
}
like image 373
MiscellaneousUser Avatar asked Jul 24 '17 08:07

MiscellaneousUser


1 Answers

One thing that can cause this, is if you click on the console window in such a way that it starts to select text, in other words, the first step in copying text out of the console window. When this happens, a write to the console window will hang until you return to the console window and press Enter to remove the selection box.

like image 75
Tony Isaac Avatar answered Sep 30 '22 09:09

Tony Isaac