How to add a Timeout to Console.ReadLine()?

2018-12-31 09:26发布

I have a console app in which I want to give the user x seconds to respond to the prompt. If no input is made after a certain period of time, program logic should continue. We assume a timeout means empty response.

What is the most straightforward way of approaching this?

30条回答
不再属于我。
2楼-- · 2018-12-31 09:49

If you're in the Main() method, you can't use await, so you'll have to use Task.WaitAny():

var task = Task.Factory.StartNew(Console.ReadLine);
var result = Task.WaitAny(new Task[] { task }, TimeSpan.FromSeconds(5)) == 0
    ? task.Result : string.Empty;

However, C# 7.1 introduces the possiblity to create an async Main() method, so it's better to use the Task.WhenAny() version whenever you have that option:

var task = Task.Factory.StartNew(Console.ReadLine);
var completedTask = await Task.WhenAny(task, Task.Delay(TimeSpan.FromSeconds(5)));
var result = object.ReferenceEquals(task, completedTask) ? task.Result : string.Empty;
查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 09:50

I came to this answer and end up doing:

    /// <summary>
    /// Reads Line from console with timeout. 
    /// </summary>
    /// <exception cref="System.TimeoutException">If user does not enter line in the specified time.</exception>
    /// <param name="timeout">Time to wait in milliseconds. Negative value will wait forever.</param>        
    /// <returns></returns>        
    public static string ReadLine(int timeout = -1)
    {
        ConsoleKeyInfo cki = new ConsoleKeyInfo();
        StringBuilder sb = new StringBuilder();

        // if user does not want to spesify a timeout
        if (timeout < 0)
            return Console.ReadLine();

        int counter = 0;

        while (true)
        {
            while (Console.KeyAvailable == false)
            {
                counter++;
                Thread.Sleep(1);
                if (counter > timeout)
                    throw new System.TimeoutException("Line was not entered in timeout specified");
            }

            cki = Console.ReadKey(false);

            if (cki.Key == ConsoleKey.Enter)
            {
                Console.WriteLine();
                return sb.ToString();
            }
            else
                sb.Append(cki.KeyChar);                
        }            
    }
查看更多
姐姐魅力值爆表
4楼-- · 2018-12-31 09:51

One way or another you do need a second thread. You could use asynchronous IO to avoid declaring your own:

  • declare a ManualResetEvent, call it "evt"
  • call System.Console.OpenStandardInput to get the input stream. Specify a callback method that will store its data and set evt.
  • call that stream's BeginRead method to start an asynchronous read operation
  • then enter a timed wait on a ManualResetEvent
  • if the wait times out, then cancel the read

If the read returns data, set the event and your main thread will continue, otherwise you'll continue after the timeout.

查看更多
永恒的永恒
5楼-- · 2018-12-31 09:51

I may be reading too much into the question, but I am assuming the wait would be similar to the boot menu where it waits 15 seconds unless you press a key. You could either use (1) a blocking function or (2) you could use a thread, an event, and a timer. The event would act as a 'continue' and would block until either the timer expired or a key was pressed.

Pseudo-code for (1) would be:

// 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);
}
查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 09:51

As if there weren't already enough answers here :0), the following encapsulates into a static method @kwl's solution above (the first one).

    public static string ConsoleReadLineWithTimeout(TimeSpan timeout)
    {
        Task<string> task = Task.Factory.StartNew(Console.ReadLine);

        string result = Task.WaitAny(new Task[] { task }, timeout) == 0
            ? task.Result 
            : string.Empty;
        return result;
    }

Usage

    static void Main()
    {
        Console.WriteLine("howdy");
        string result = ConsoleReadLineWithTimeout(TimeSpan.FromSeconds(8.5));
        Console.WriteLine("bye");
    }
查看更多
孤独寂梦人
7楼-- · 2018-12-31 09:53

I think you will need to make a secondary thread and poll for a key on the console. I know of no built in way to accomplish this.

查看更多
登录 后发表回答