TomaszRewak / C-sharp-console-gui-framework

A GUI framework for C# console applications
MIT License
1.08k stars 45 forks source link

IOException thrown when calling AdjustWindowSize #20

Closed rpwjanzen closed 4 years ago

rpwjanzen commented 4 years ago

Calling ConsoleManager.AdjustWindowSize() in a tight loop while resizing the window with the mouse can cause a System.IO.IOException to be thrown.

Exception Details

System.IO.IOException: The parameter is incorrect.
   at System.ConsolePal.SetCursorPosition(Int32 left, Int32 top)
   at ConsoleGUI.Api.StandardConsole.set_Size(Size value)
   at ConsoleGUI.ConsoleManager.Resize(Size& size)
   at ConsoleGUI.ConsoleManager.AdjustWindowSize()

Might want to add a SafeConsole.SetCursorPosition method.

TomaszRewak commented 4 years ago

I wasn't able to reproduce it on my side. What OS/terminal are you using?

But nevertheless, adding a SafeConsole.SetCursorPosition seems like a good idea regardless. Those console APIs are bit wonky.

TomaszRewak commented 4 years ago

Should be fixed in v1.2.1 by 1863c5c6cd51304aa604d3a021686d945d566b37 Please let me know if you can reproduce this issue with the new version of the package.

rpwjanzen commented 4 years ago

I am using the "Command Prompt" (cmd.exe) terminal on Windows 10 version 1903 (Build 18362.1139).

Below is an example of code that will reproduce the issue.

    // while running, resize the window to be really small
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleManager.Setup();
            ConsoleManager.Content = new TextBlock
            {
                Text = "Text Block"
            };

            while (true)
            {
                ConsoleManager.AdjustWindowSize();
                System.Threading.Thread.Yield();
            }
        }
    }

The stack trace

   at System.ConsolePal.SetCursorPosition(Int32 left, Int32 top)
   at ConsoleGUI.Api.SimplifiedConsole.Write(Position position, Character& character) in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\Api\SimplifiedConsole.cs:line 24
   at ConsoleGUI.ConsoleManager.Update(Rect rect) in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI   at System.ConsolePal.SetCursorPosition(Int32 left, Int32 top)
   at ConsoleGUI.Api.SimplifiedConsole.Write(Position position, Character& character) in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\Api\SimplifiedConsole.cs:line 24
   at ConsoleGUI.ConsoleManager.Update(Rect rect) in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\ConsoleManager.cs:line 148
   at ConsoleGUI.ConsoleManager.Redraw() in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\ConsoleManager.cs:line 126
   at ConsoleGUI.ConsoleManager.Initialize() in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\ConsoleManager.cs:line 121
   at ConsoleGUI.ConsoleManager.Resize(Size& size) in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\ConsoleManager.cs:line 168
   at ConsoleGUI.ConsoleManager.AdjustWindowSize() in C:\Users\...\C-sharp-console-gui-framework\ConsoleGUI\ConsoleManager.cs:line 180
   at Repro.Program.Main(String[] args) in C:\Users\...\Repro\Program.cs:line 25
TomaszRewak commented 4 years ago

I think I was able to reproduce it on my end as well. Can you please confirm that it only happens when the window is resized to a point where it's completely collapsed? image Or does it happen also where there is some drawind space visible?

TomaszRewak commented 4 years ago

Should be fixed in v1.2.2 by 42fd2f84a4ea86912c34f2813593a41fde9461c7

rpwjanzen commented 4 years ago

This issue does occur when there is some drawing space visible. It appears to happen when the Console.SetCursorPosition(_, 0) is called. v1.2.2 appears to have fixed the issue.

TomaszRewak commented 4 years ago

Interesting, on my side it was only happening when the window was 0px tall. But v1.2.2 should take care of that and all other edge cases.

I'm closing the ticket.

Thanks for reporting and the help in investigating :)