调整控制台事件大小 [英] Resize Event for Console

查看:217
本文介绍了调整控制台事件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我认为窗口调整大小的事件将通过winproc,我可能会误会,希望得到一个控制台调整大小事件的通知。

So I thought the window resize event would come through winproc, I might be mistaken, looking to get notified for a Console resize event.

我想最大化调整缓冲区的大小,一旦完成,它基本上缩小到窗口大小,从而防止由于缓冲区小于窗口溢出错误。

I want to maximize the console buffer on resize, and once it's finished essentially shrink it back down to the window size, thus preventing an overflow error due to the buffer being smaller than the window.

推荐答案

不幸的是,答案是你无法挂接控制台的WndProc,因为它是在一个单独的,受控程序。 Spy ++可以钩住其他进程的WndProcs来读取窗口消息,Spy ++甚至不能读取控制台窗口消息。即使您可以解决安全问题,C#也不能用于挂钩另一个进程。

Unfortunately, the answer is you can't hook the console's WndProc because it's in a separate, security-controlled process. Spy++ can hook other processes' WndProcs to read window messages, and Spy++ can't even read console window messages. Even if you could work around the security issue, C# cannot be used to hook another process.

我在调整大小时具有完全相同的竞争条件。在Windows 10上更糟糕,因为调整窗口大小会重新排列文本,并更改​​缓冲区宽度以及窗口宽度。 Console.BufferWidth Console.BufferHeight 和家族是非原子的,如果您使用的话将抛出管理错误和非托管错误

I'm having the exact same race condition on resize. It's worse on Windows 10 because resizing the window reflows the text and changes the buffer width as well as the window width. Console.BufferWidth, Console.BufferHeight and family are non-atomic and will throw both managed and unmanaged errors if you use them while the window is being resized.

您可以在后调整大小的大小msdn.microsoft.com/en-us/library/windows/desktop/ms685035.aspxrel =nofollow noreferrer>读取输入缓冲区事件,但这不会解决您的问题。您仍然会出现并发问题。既然这不是一个钩子,那么你不能使调整大小等待你完成控制台类的非原子缓冲区/窗口大小操作。

You can definitely find out about resizes after the fact by Reading Input Buffer Events but that won't solve your problem. You'll still have concurrency issues. Since that's not a hook, you can't make the resize wait for you to finish the Console class's non-atomic buffer/window size operations.

我认为处理竞争条件的唯一选择是重试循环,这就是我将要使用的。

I think the only option to deal with the race condition is a retry loop, and that is what I will use.

while (true) try
{
    // Read and write the buffer size/location and window size/location and cursor position,
    // but be aware it will be rudely interrupted if the console is resized by the user.
}
catch (IOException) { }
catch (ArgumentOutOfRangeException) { }

这篇关于调整控制台事件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆