如何继续按直到任何键 [英] How to continue until press any key

查看:88
本文介绍了如何继续按直到任何键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在控制台应用程序中没有按任何键之前连接程序

How to contonue program until not press any key in console application

Do
    .
    .
    .
Loop while <press any key>

推荐答案

地狱,不!你的循环是轮询,被认为是一件非常糟糕的事情,尤其是在UI中。你需要的是:



Hell, no! Your loop is polling and is considered a very bad thing, especially in UI. What you need is this:

System.Console.Write("Press any key...");
System.Console.ReadKey(true);





ReadKey 调用正在阻止。您的线程将处于等待状态,浪费不占用CPU时间,只有在您实际按下某个键时才会被唤醒,一直触发OS内核中的硬件中断(或者如果您中止线程或整个过程)。 br $>


-SA



The ReadKey call is blocking. Your thread will be put in wait state wasting not CPU time, will be waken up only when you actually press a key, all the way from triggering a hardware interrupt in OS kernel (or if you abort the thread or the whole process).

—SA


这篇关于如何继续按直到任何键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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