控制台应用程序-在c#中从标准I/O读取非阻塞
本文关键字:读取 标准 应用程序 控制台 | 更新日期: 2023-09-27 17:49:45
我想要一个来自控制台的非阻塞读取函数。我怎么用c#写呢?
Richard Dutton在他的博客上有一个解决方案:
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.F1:
Console.WriteLine("You pressed F1!");
break;
default:
break;
}
}
// Do something more useful
}
var buf=new byte[2048];
var inputStream=Console.OpenStandardInput(); //dispose me when you're done
inputStream.BeginRead(buf,0,buf.Length,ar=>{
int amtRead=inputStream.EndRead(ar);
//buf has what you need. You'll need to decode it though
},null);