如何向Console.ReadLine()添加超时?
我有一个控制台应用程序,我想在其中给用户x秒的时间来响应提示。如果在一定时间后没有输入,则程序逻辑应继续。我们假设超时意味着空响应。
解决这个问题的最直接方法是什么?
解决方案
回答
我认为我们将需要创建一个辅助线程并在控制台上轮询密钥。我知道没有内置的方法可以完成此任务。
回答
这种使用Console.KeyAvailable的方法会有所帮助吗?
class Sample { public static void Main() { ConsoleKeyInfo cki = new ConsoleKeyInfo(); do { Console.WriteLine("\nPress a key to display; press the 'x' key to quit."); // Your code could perform some useful task in the following loop. However, // for the sake of this example we'll merely pause for a quarter second. while (Console.KeyAvailable == false) Thread.Sleep(250); // Loop until input is entered. cki = Console.ReadKey(true); Console.WriteLine("You pressed the '{0}' key.", cki.Key); } while(cki.Key != ConsoleKey.X); } }
回答
一种或者另一种方法确实需要第二个线程。我们可以使用异步IO来避免声明自己的:
- 声明一个ManualResetEvent,将其称为" evt"
- 调用System.Console.OpenStandardInput以获取输入流。指定将存储其数据并设置evt的回调方法。
- 调用该流的BeginRead方法以启动异步读取操作
- 然后在ManualResetEvent上输入定时等待
- 如果等待超时,则取消读取
如果读取返回数据,则设置事件,主线程将继续,否则将在超时后继续。
回答
获得第二个线程的另一种便宜的方法是将其包装在委托中。
回答
编辑:通过在单独的进程中完成实际工作并在超时的情况下终止该进程来解决该问题。有关详情,请参见下文。 ew!
只是运行了一下,它似乎运行良好。我的同事有一个使用Thread对象的版本,但是我发现委托类型的BeginInvoke()方法更加优雅。
namespace TimedReadLine { public static class Console { private delegate string ReadLineInvoker(); public static string ReadLine(int timeout) { return ReadLine(timeout, null); } public static string ReadLine(int timeout, string @default) { using (var process = new System.Diagnostics.Process { StartInfo = { FileName = "ReadLine.exe", RedirectStandardOutput = true, UseShellExecute = false } }) { process.Start(); var rli = new ReadLineInvoker(process.StandardOutput.ReadLine); var iar = rli.BeginInvoke(null, null); if (!iar.AsyncWaitHandle.WaitOne(new System.TimeSpan(0, 0, timeout))) { process.Kill(); return @default; } return rli.EndInvoke(iar); } } } }
ReadLine.exe项目是一个非常简单的项目,具有一个如下所示的类:
namespace ReadLine { internal static class Program { private static void Main() { System.Console.WriteLine(System.Console.ReadLine()); } } }
回答
在委托中调用Console.ReadLine()很糟糕,因为如果用户未点击" enter",则该调用将永远不会返回。执行委托的线程将被阻塞,直到用户单击" enter"为止,而无法取消它。
发出一系列这些调用将不会像我们期望的那样运行。考虑以下内容(使用上面的示例Console类):
System.Console.WriteLine("Enter your first name [John]:"); string firstName = Console.ReadLine(5, "John"); System.Console.WriteLine("Enter your last name [Doe]:"); string lastName = Console.ReadLine(5, "Doe");
用户让第一个提示的超时时间到期,然后为第二个提示输入一个值。 firstName和lastName都将包含默认值。当用户单击" enter"时,将完成第一个ReadLine调用,但是代码已放弃该调用,并实际上丢弃了结果。第二个ReadLine调用将继续阻塞,超时最终将到期,并且返回的值将再次为默认值。
顺便说一句,上面的代码中有一个错误。通过调用waitHandle.Close(),可以从工作线程下关闭事件。如果用户在超时时间过后单击" enter",则工作线程将尝试发出信号,该事件引发ObjectDisposedException。异常是从工作线程中抛出的,如果我们尚未设置未处理的异常处理程序,则该过程将终止。
回答
我可能对这个问题读得太多,但是我假设等待的过程类似于启动菜单,在该菜单中,等待15秒,除非我们按任何键。我们可以使用(1)阻塞函数,也可以使用(2)使用线程,事件和计时器。该事件将充当"继续"事件,并将一直阻塞到计时器到期或者按键被按下为止。
(1)的伪代码为:
// Get configurable wait time TimeSpan waitTime = TimeSpan.FromSeconds(15.0); int configWaitTimeSec; if (int.TryParse(ConfigManager.AppSetting["DefaultWaitTime"], out configWaitTimeSec)) waitTime = TimeSpan.FromSeconds(configWaitTimeSec); bool keyPressed = false; DateTime expireTime = DateTime.Now + waitTime; // Timer and key processor ConsoleKeyInfo cki; // EDIT: adding a missing ! below while (!keyPressed && (DateTime.Now < expireTime)) { if (Console.KeyAvailable) { cki = Console.ReadKey(true); // TODO: Process key keyPressed = true; } Thread.Sleep(10); }
回答
上面的Eric帖子的示例实现。此特定示例用于读取通过管道传递到控制台应用程序的信息:
using System; using System.Collections.Generic; using System.IO; using System.Threading; namespace PipedInfo { class Program { static void Main(string[] args) { StreamReader buffer = ReadPipedInfo(); Console.WriteLine(buffer.ReadToEnd()); } #region ReadPipedInfo public static StreamReader ReadPipedInfo() { //call with a default value of 5 milliseconds return ReadPipedInfo(5); } public static StreamReader ReadPipedInfo(int waitTimeInMilliseconds) { //allocate the class we're going to callback to ReadPipedInfoCallback callbackClass = new ReadPipedInfoCallback(); //to indicate read complete or timeout AutoResetEvent readCompleteEvent = new AutoResetEvent(false); //open the StdIn so that we can read against it asynchronously Stream stdIn = Console.OpenStandardInput(); //allocate a one-byte buffer, we're going to read off the stream one byte at a time byte[] singleByteBuffer = new byte[1]; //allocate a list of an arbitary size to store the read bytes List<byte> byteStorage = new List<byte>(4096); IAsyncResult asyncRead = null; int readLength = 0; //the bytes we have successfully read do { //perform the read and wait until it finishes, unless it's already finished asyncRead = stdIn.BeginRead(singleByteBuffer, 0, singleByteBuffer.Length, new AsyncCallback(callbackClass.ReadCallback), readCompleteEvent); if (!asyncRead.CompletedSynchronously) readCompleteEvent.WaitOne(waitTimeInMilliseconds); //end the async call, one way or another //if our read succeeded we store the byte we read if (asyncRead.IsCompleted) { readLength = stdIn.EndRead(asyncRead); if (readLength > 0) byteStorage.Add(singleByteBuffer[0]); } } while (asyncRead.IsCompleted && readLength > 0); //we keep reading until we fail or read nothing //return results, if we read zero bytes the buffer will return empty return new StreamReader(new MemoryStream(byteStorage.ToArray(), 0, byteStorage.Count)); } private class ReadPipedInfoCallback { public void ReadCallback(IAsyncResult asyncResult) { //pull the user-defined variable and strobe the event, the read finished successfully AutoResetEvent readCompleteEvent = asyncResult.AsyncState as AutoResetEvent; readCompleteEvent.Set(); } } #endregion ReadPipedInfo } }
回答
string ReadLine(int timeoutms) { ReadLineDelegate d = Console.ReadLine; IAsyncResult result = d.BeginInvoke(null, null); result.AsyncWaitHandle.WaitOne(timeoutms);//timeout e.g. 15000 for 15 secs if (result.IsCompleted) { string resultstr = d.EndInvoke(result); Console.WriteLine("Read: " + resultstr); return resultstr; } else { Console.WriteLine("Timed out!"); throw new TimedoutException("Timed Out!"); } } delegate string ReadLineDelegate();
回答
简单的线程示例来解决这个问题
Thread readKeyThread = new Thread(ReadKeyMethod); static ConsoleKeyInfo cki = null; void Main() { readKeyThread.Start(); bool keyEntered = false; for(int ii = 0; ii < 10; ii++) { Thread.Sleep(1000); if(readKeyThread.ThreadState == ThreadState.Stopped) keyEntered = true; } if(keyEntered) { //do your stuff for a key entered } } void ReadKeyMethod() { cki = Console.ReadKey(); }
或者顶部的静态字符串以获取整行。
回答
不幸的是,我无法评论Gulzar的帖子,但这是一个更完整的示例:
while (Console.KeyAvailable == false) { Thread.Sleep(250); i++; if (i > 3) throw new Exception("Timedout waiting for input."); } input = Console.ReadLine();
回答
我的情况下,这个工作很好:
public static ManualResetEvent evtToWait = new ManualResetEvent(false); private static void ReadDataFromConsole( object state ) { Console.WriteLine("Enter \"x\" to exit or wait for 5 seconds."); while (Console.ReadKey().KeyChar != 'x') { Console.Out.WriteLine(""); Console.Out.WriteLine("Enter again!"); } evtToWait.Set(); } static void Main(string[] args) { Thread status = new Thread(ReadDataFromConsole); status.Start(); evtToWait = new ManualResetEvent(false); evtToWait.WaitOne(5000); // wait for evtToWait.Set() or timeOut status.Abort(); // exit anyway return; }
回答
// Wait for 'Enter' to be pressed or 5 seconds to elapse using (Stream s = Console.OpenStandardInput()) { ManualResetEvent stop_waiting = new ManualResetEvent(false); s.BeginRead(new Byte[1], 0, 1, ar => stop_waiting.Set(), null); // ...do anything else, or simply... stop_waiting.WaitOne(5000); // If desired, other threads could also set 'stop_waiting' // Disposing the stream cancels the async read operation. It can be // re-opened if needed. }