听按键的背景过程 [英] Background process for listening for keypress

查看:64
本文介绍了听按键的背景过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ac #console telnet echoing程序(它连接到PABX和echo s数据到控制台窗口)。 

I have a c# console telnet echoing program (it connects to a PABX and echo s data to the console window). 

我想要添加到代码中的是它能够在代码运行时监听键盘按键,并根据键/序列,终止程序(Ctrl Q)暂停/恢复(Ctrl-P)?

What I would like to add to the code is the ability for it to listen for a keyboard press while the code is running, and depending on the key/sequence, either terminate the program (Ctrl Q) ore pause/Resume (Ctrl-P)?

如果环顾四周并找到 

If had a look around and found 

https://stackoverflow.com/questions/5891538/ listen-for-key-press-in-net-console-app

https://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app

https://stackoverflow.com/questions/20845945/readkey-while-key-is-not-pressed-做什么

https://stackoverflow.com/questions/20845945/readkey-while-key-is-not-pressed-do-something

但是我无法使用我的代码。当我这样做时,我按下ESC启动程序,但是当它正在监听TCP服务器时,ti会工作,或者,当我移动按键监听代码时,它只是逃避程序而不先运行它。

But I am unable to get it to work with my code. when I do, I have the press ESC to start the program, but ti would work while it's listening to the TCP server, or, when I move the keypress listening code, it just escapes the program without running it first.

//var input = Console.ReadKey();
//do
//{

TcpClient client =新的TcpClient(服务器,端口);

TcpClient client = new TcpClient(server, port);

NetworkStream stream = client.GetStream();
treamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream){AutoFlush = true};
stream.Flush();

while(client.Connected)
{
string DataReceived = reader.ReadLine();
Console.WriteLine(DataReceived);
string newTxtLine = DataReceived + Environment.NewLine;
}

NetworkStream stream = client.GetStream(); treamReader reader = new StreamReader(stream); StreamWriter writer = new StreamWriter(stream) { AutoFlush = true }; stream.Flush(); while (client.Connected) { string DataReceived = reader.ReadLine(); Console.WriteLine(DataReceived); string newTxtLine = DataReceived + Environment.NewLine; }

//}

有人可以指出id a)这是在代码运行时用于监听按键的正确代码/过程,以及b)我如何实现它?

Could someone point out id a) this is the correct code/procedure to use to listen for a keypress while the code is running, and b) how I implement it?

谢谢

推荐答案

为了响应任意按键你将会必须使用多个线程。主线程将监听击键。辅助线程必须处理TCP通信。这是同时执行2个同步操作的唯一方法。
但请注意,TcpClient确实支持使用Begin调用的异步操作。这可以明确减少对多个线程的需求,但是使用多个线程可能更容易。 网上有很多关于如何正确执行
的例子。

In order to respond to arbitrary keypresses you'll have to use multiple threads. The main thread will listen for keystrokes. The secondary thread will have to handle the TCP communication. This is the only way to execute 2 sync operations at the same time. But note that TcpClient does support async operations using the Begin calls. This may alleviate the need for multiple threads explicitly but it is probably easier to just use multiple threads.  There are plenty of examples online on how to properly do this.

还要注意"终止"的标准方法。控制台应用程序是Ctrl + C. Console类上有一个特殊事件,您可以为此加入。因此,为了支持取消,您应该只处理此事件。它会被触发
与你是否有一个线程无关。这是取消控制台应用程序的首选方法。但是您的代码需要处理取消请求(通常通过CancellationTokenSource或Event)。

Note also that the standard way to "terminate" a console app is Ctrl+C. There is a special event on the Console class that you can hook into for this. So for supporting cancellation you should simply handle this event. It will get triggered irrelevant of whether you have a single thread or not. This is the preferred approach for cancelling a console app. But your code needs to handle the cancellation request (generally via CancellationTokenSource or Event).


这篇关于听按键的背景过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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