非阻塞控制台输入. [英] Non-blocking console input.

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

问题描述

你好,

我的申请有一个小问题.这是一个控制台应用程序.

当连接到服务器时,该应用程序会收到大量数据,并且我不能长时间阻止连接.因此,我创建了另一个处理消息的线程(GetMessage).因此,现在某些数据包会调用需要用户输入的操作.我将PostThreadMessageA与自定义消息一起使用,以便用户可以填写信息,并且recv线程不会被std :: cin阻止.
效果很好.

但是现在我想添加一些命令处理程序,例如".resenddata".我会为此创建另一个线程,但是它不能与GetMessage线程一起很好地工作(两个std :: cin在2个不同的线程中同时调用时效果不佳).

任何想法如何解决这种情况.

我想提醒一些注意事项:
-Recv线程不得被用户输入阻止,因为服务器始终会向您发送数据.
-一些收到的数据包需要用户响应.例如验证请求".
-我希望能够通过命令控制应用程序的某些操作.

感谢您的帮助和想法.

Hello,

I have a little problem with my application. It''s an console application.

The application receives a lot of data when connected to server and I cannot block the connection for too long time. So I created another thread which handles the messages (GetMessage). So now certain data packets invokes action which needs user input. I use PostThreadMessageA with custom messages so the user can fill in information and the recv thread isn''t blocked by std::cin.
That works quite well.

But now I would like to add some command handler like ".resenddata". I would create another thread for that, but it wouldn''t work well with GetMessage thread (Two std::cin doesn''t work very good when called at the same time in the 2 different threads).

Any idea how to solve this situation.

Some points I would like to remind:
- The Recv thread must not be blocked by user input because server sends you data all the time.
- Some received packets requires user response. For example "Auth request".
- I want to be able to control some actions of the application by commands.

Thank you for your help and ideas.

推荐答案

如果仅写控制台,则可以轻松地将线程与控制台应用程序一起使用.该线程将共享控制台以进行输出,并以某种方式混合输出线.您可以很好地处理它,并使用临界区"来互锁文本块.这就是经常展示线程技术的方式.

从技术上讲,您也可以使用控制台输入来执行此操作,但是它将使控制台极为混乱:将在您键入内容时添加字符串并向下滚动控制台.严肃的控制台应用程序很少使用控制台输入.如果使用线程编写来控制台,请避免输入.您可以改用命令行参数.

如果需要交互式输入和多线程控制台输出,请考虑创建一个窗口化的应用程序.在一个控件中模拟控制台输出(列表视图是保留输出行的理想选择),并使用常规输入控件与模拟控制台分开组织输入.您可以创建控制台复制/粘贴,数据验证和其他不错的功能.

—SA
You can easily use threads with console application if you only write to console. The thread will share the console for output, mixing the output lines somehow. You can handle it nicely and use Critical Section to interlock blocks of text. This is how threading techniques are often demonstrated.

Technically, you can do it with console input as well, but it will make the console extremely messy: strings will be added and console scrolled down as you type. Serious console application rarely use console input. If you use threads writing to console, avoid input. You can use command line parameters instead.

If you need interactive input with multithreaded console output, consider creating a windowed application. Simulate console output in one control (a list view is a perfect candidate for keeping output lines), and organize input separately from you simulated console using regular input control. You can create console copy/paste, data validation and other nice feature.

—SA


如果我正确理解这一点,则您有一个工作的控制台应用程序,可以在一个窗口中处理接收到的数据和所需的用户响应.

是吗?

而且您遇到的问题是您想让用户也可以输入其他命令,但是您不能使用std :: cin进行输入,因为该输入已经被所需的用户使用了.回应.

如果要将其保留为控制台应用程序,则需要不同的流作为命令处理程序的输入.

尝试启动第二个控制台窗口并执行命令"copy con tmpfile.txt",无论您在该控制台窗口中键入的内容是什么,每次您按回车键时都会写入tmpfile.txt(直到您按^ Z表示文件结束).

然后,您应该能够从控制台应用程序中的tmpfile.txt中读取并将其用作命令输入流.我不确定tmpfile.txt上的文件结尾如何出现在控制台应用程序中,因此您可能必须循环测试tmpfile.txt上文件大小的变化.

如果这对您有用,那么您可以添加让您的控制台应用程序实际生成另一个命令窗口,以便在启动时执行"copy con tmpfile.txt".

这样就可以编写Windows应用程序了.

(坦率地说,一旦您熟悉Windows应用程序,就不必编写太多代码了,但是,如果您只是刚开始使用或来自UNIX背景,那么我概述的方法应该适用于您需要.)
If I understand this correctly, you have a working console application that handles the received data and the required user response in one window.

Is that right?

And the problem you are running into is that you''d like to have a way for the user to also input additional commands, but you can''t use std::cin for that input because that is already being used for required user responses.

If you want to keep this as a console application, then you need a different stream for the input to the command handler.

Try starting a second console window and execute the command "copy con tmpfile.txt" whatever you type in that console window will be written to tmpfile.txt each time you hit return (until you hit ^Z to signal end of file).

You should then be able to read from tmpfile.txt in your console application and use that as your command input stream. I''m not sure how end-of-file on tmpfile.txt will appear in your console application, so you might have to loop and test for the file size changing on tmpfile.txt.

If that works for you, then you could add have your console application actually spawn the other command window to execute "copy con tmpfile.txt" at startup.

That will get around writing a windows application.

(Frankly, once you get familiar with windows applications, it''s not that much more code to write, but if you are just starting out or are coming from a unix background, then the approach I''ve outlined should work for what you need.)


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

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