Console.WriteLine(“”); gets stuck

2020-04-02 13:23发布

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;
}

3条回答
祖国的老花朵
2楼-- · 2020-04-02 14:05

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.

查看更多
▲ chillily
3楼-- · 2020-04-02 14:14

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.

查看更多
Animai°情兽
4楼-- · 2020-04-02 14:16

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

查看更多
登录 后发表回答