32

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;
}
juanferrer
  • 1,192
  • 1
  • 16
  • 29
MiscellaneousUser
  • 2,915
  • 4
  • 25
  • 44
  • 9
    You haven't shown any code so it's impossible to help. It's not this by any chance: https://stackoverflow.com/questions/30418886/how-and-why-does-quickedit-mode-in-command-prompt-freeze-applications ? – DavidG Jul 24 '17 at 08:19
  • Nope, not that issue, its a Console Application being developed in C# debugged in Visual Studio 2017. It stalls on the line in the title. – MiscellaneousUser Jul 24 '17 at 08:24
  • `“”` arent the right ones :P jk but no code = no help because normal writeline will not stall the application – EpicKip Jul 24 '17 at 08:25
  • @EpicKip The quotes are because that's how SO renders the titles! – DavidG Jul 24 '17 at 08:26
  • You don't have `oFptr`, are you sure you have such object somewhere in the code and your program is not stuck on `oFptr` ? have you tried step debugging ? – Markiian Benovskyi Jul 24 '17 at 09:31
  • oFptr is not a problem, i have debugged it. When I break the code, the line it breaks on is the writeline and i can't set next statement. When I press F10 to continue, nothing happens, then when i break again, it still on the same line. – MiscellaneousUser Jul 24 '17 at 10:01
  • Is the output redirected to another application? – wimh Aug 26 '17 at 14:12

3 Answers3

144

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.

Tony Isaac
  • 1,546
  • 1
  • 9
  • 6
  • 10
    This is exactly what my issue was. – Turnercj65 Oct 31 '17 at 17:04
  • 4
    [How to programmatic disable C# Console Application's Quick Edit mode?](https://stackoverflow.com/questions/13656846/how-to-programmatic-disable-c-sharp-console-applications-quick-edit-mode) discussion contains a method to disable text selection and prevent such hangs. – Leonid Vasilev Feb 22 '18 at 10:06
  • Ran into the same issue. This is VERY weird to me. Any idea why it happens? – FallenAvatar Jun 11 '18 at 13:03
  • Tony, I know this is an old question, it happened again today. I remembered my question and noticed it wouldn't budge, saying something about a thread. Nothing on my screen selecting text so I kept pressing enter and selecting and unselecting text and eventually it responded. – MiscellaneousUser Aug 06 '18 at 15:44
0

Using the System.Threading.Tasks.Dataflow nuget package, you can use a buffer to help with not locking the application in the event the user selects text on the console window

    private static BufferBlock<string> _buffer = new BufferBlock<string>();
    private static Task _consumer;
    private static CancellationTokenSource _cts;

    public static void Main(string[] args)
    {
        _buffer = new BufferBlock<string>();
        _cts = new CancellationTokenSource();
        _consumer = ConsumeAsync(_buffer, _cts.Token);

        for (int i = 0; i < 100000; i++)
        {
            WriteToConsole(i.ToString());
        }

        Console.ReadLine();
    }

    private static void WriteToConsole(string message)
    {
        SendToBuffer(_buffer, message);
    }

    private static void SendToBuffer(ITargetBlock<string> target, string message)
    {
        target.Post(message);
    }

    private static async Task ConsumeAsync(IReceivableSourceBlock<string> source, CancellationToken cancellationToken)
    {
        while (await source.OutputAvailableAsync(cancellationToken))
        {
            var message = await source.ReceiveAsync();

            Console.WriteLine(message);
        }
    }

This means writing to console won't block at all, so it's probably not useful in a lot of scenarios. In my case I just needed to spit out logging info

p3tch
  • 1,414
  • 13
  • 29
-1

It also happened to be, and even sometimes when i debug my code it didnt start (just frozen). Sometimes i needed to stop the execution and redebug my code. I deleted visual studio and reinstalled and all is well now. I advice you to do the same.

  • Thats a shame, I was hoping it was some sort of buffering issue, one that I could flush or do something like that. – MiscellaneousUser Jul 24 '17 at 10:02
  • Maybe there's another way to solve this problem like you said but i found it easier to just reinstalling visual studio –  Jul 24 '17 at 10:10