异步控制台输出 [英] Async Console Output

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

问题描述

我的应用程序win32控制台有问题。

I have a problem with my application win32 console.

控制台用于向我的应用程序发出命令。但是,同时它用于输出大多数来自异步线程的日志消息。当用户尝试写入一些输入并同时打印一个异步日志消息时,这就成为一个问题,从而颠覆了用户输入的显示。

The console is used to give commands to my application. However, at the same time it is used to output log messages which mostly comes from asynchronous threads. This becomes a problem when the user tries to write some input and simultaneously an async log message is printed, thus thrashing the display of the users input.

我想要一些关于如何处理这种情况的建议?

I would like to have some advice in regards to how to handle such a situtation?

例如,可以将控制台中的最后一行用于输入,类似于它在

Is it possible for example to dedicate the last line in the console to input, similarly to how it looks in the in-game consoles for some games?

推荐答案

这是可能使用控制台API,但它涉及相当多的工作,所有使用控制台的线程必须通过调用您的输出方法而不是直接调用控制台API函数或运行时库输出函数来协作。

This is possible using the Console API, but it involves quite a bit of work and all the threads that use the console will have to cooperate by calling your output method rather than directly calling the Console API functions or the runtime library output functions.

基本思想是让您的公共输出函数写入控制台屏幕缓冲区,并在代码中滚动缓冲区,而不是让文本流到最后一行,并自动滚动。我记得,你必须解析换行符和其他控制字符的输出,并正确处理。

The basic idea is to have your common output function write to the console screen buffer, and scroll the buffer in code rather than letting the text flow onto the last line and scroll automatically. As I recall, you'll have to parse the output for newlines and other control characters, and handle them correctly.

可能在最后一行上使用cooked控制台输入,即使这样做,如果用户输入的文本比单个行上的文本多,则会出现问题。此外,用户在行尾输入Enter可能会导致向上滚动。可能最好在这种情况下使用原始控制台输入。

You might be able to get away with using "cooked" console input on the last line, although in doing so you risk problems if the user enters more text than will fit on a single line. Also, the user hitting Enter at the end of the line might cause it to scroll up. Probably best in this situation to use raw console input.

你会想变得非常熟悉 Windows控制台

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

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