清除控制台缓冲区 [英] Clear Console Buffer

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

问题描述

我正在VS2008中编写一个示例控制台应用程序。现在,我有一个 Console.WriteLine()方法,该方法在屏幕上显示输出,然后有 Console.ReadKey()等待用户结束应用程序。

I'm writing a sample console application in VS2008. Now I have a Console.WriteLine() method which displays output on the screen and then there is Console.ReadKey() which waits for the user to end the application.

如果在显示 Console.WriteLine()方法时按 Enter 应用程序退出。

If I press Enter while the Console.WriteLine() method is displaying then the application exits.

如何在 Console.ReadKey()方法之前清除输入缓冲区,所以无论有多少次用户在显示数据时按下Enter键, Console.ReadKey()方法应该停止应用程序退出吗?

How can I clear the input buffer before the Console.ReadKey() method so that no matter how many times the user presses the Enter button while the data is being displayed, the Console.ReadKey() method should stop the application from exiting?

推荐答案

while(Console.KeyAvailable) 
{
    Console.ReadKey(false);
}
Console.ReadKey();

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

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