如何在无限循环运行时读取iinput? [英] How to read an iinput while an infinite loop is running?

查看:77
本文介绍了如何在无限循环运行时读取iinput?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中运行无限循环时接受输入。但我不想停止我的程序。我应该怎么做。



我尝试过:



plzz帮助我!!!!!!我不想使用getch功能。如果有可能那么plzz告诉我如何用kbhit函数解决这个问题。

i want to take an input while an infinite loop is running in C++ .but i dont want ot halt my programme .what should i do.

What I have tried:

plzz help me out!!!!!! i dont want to use getch function . if it is possible then plzz tell me how can i solve this with kbhit function.

推荐答案

kbhit如果不一定要走的路,对于初学者来说它不起作用在所有环境中 - kbhit | Microsoft Docs [ ^ ]:
kbhit if not necessarily the way to go, for starters it doesn't work in all environments - kbhit | Microsoft Docs[^]:
Quote:

此API不能在Windows运行时中执行的应用程序中使用。有关更多信息,请参阅/ ZW不支持的CRT函数。

This API cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW.



即使您可以使用它,也会折旧,您需要 _ kbhit | Microsoft Docs [ ^ ]相反。



你绕过你的循环时通过调用它来使用它,如果是真的,做一些事情(读取字符为例子)这可能会导致你退出循环。



但这是一个糟糕的解决方案:使用大锤破解坚果。

什么你应该做的是在一个单独的线程中运行你的长时间运行的任务,这样你的UI - 无论你运行的UI环境 - 都不会被循环阻塞。

查看文档中的你的系统线程化,看看你是否能找到一些例子 - 我会指出你的一些,但我不知道你想要在哪个环境中工作!


Even if you can use it, it is depreciated, and you would need _kbhit | Microsoft Docs[^] instead.

You use it by calling it at intervals while you go round your loop, and if true, doing something (reading the character for example) which may cause you to exit the loop.

But it's a bad solution: using a sledgehammer to crack a nut.
What you should be doing is running your long running task in a separate thread so that your UI - whatever UI environment you are running under - does not become "blocked" by the loop.
Look under the documentation for your system for "threading" and see if you can find some examples - I'd point you at some, but I have no idea what environment you are trying to work in!


这篇关于如何在无限循环运行时读取iinput?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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