第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何向Console.ReadLine()添加超時(shí)?

如何向Console.ReadLine()添加超時(shí)?

炎炎設(shè)計(jì) 2019-06-29 10:19:01
如何向Console.ReadLine()添加超時(shí)?我有一個(gè)控制臺(tái)應(yīng)用程序,我想在其中給用戶x秒來響應(yīng)提示符。如果在一段時(shí)間后沒有輸入,則程序邏輯應(yīng)該繼續(xù)。我們假設(shè)超時(shí)意味著空響應(yīng)。最直截了當(dāng)?shù)姆椒ㄊ鞘裁矗?
查看完整描述

3 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

我驚訝地獲悉,五年后,所有答案仍然存在以下一個(gè)或多個(gè)問題:

  • 使用的是ReadLine以外的函數(shù),導(dǎo)致功能丟失。(刪除/退格/向上鍵用于先前的輸入)。
  • 函數(shù)在多次調(diào)用(產(chǎn)生多個(gè)線程、許多掛起的ReadLine或其他意外行為)時(shí)表現(xiàn)不好。
  • 函數(shù)依賴于繁忙的等待。這是一種可怕的浪費(fèi),因?yàn)榈却黄谕谌魏蔚胤竭\(yùn)行,從幾秒鐘到超時(shí),這可能是多分鐘。一個(gè)繁忙的等待,跑了這么長的時(shí)間,是一個(gè)可怕的吸資源,這是特別糟糕的多線程場景。如果用睡眠來修改繁忙-等待,這會(huì)對(duì)響應(yīng)性產(chǎn)生負(fù)面影響,盡管我承認(rèn)這可能不是什么大問題。

我相信我的解決辦法可以解決原來的問題,而不會(huì)出現(xiàn)上述任何問題:

class Reader {
  private static Thread inputThread;
  private static AutoResetEvent getInput, gotInput;
  private static string input;

  static Reader() {
    getInput = new AutoResetEvent(false);
    gotInput = new AutoResetEvent(false);
    inputThread = new Thread(reader);
    inputThread.IsBackground = true;
    inputThread.Start();
  }

  private static void reader() {
    while (true) {
      getInput.WaitOne();
      input = Console.ReadLine();
      gotInput.Set();
    }
  }

  // omit the parameter to read a line without a timeout
  public static string ReadLine(int timeOutMillisecs = Timeout.Infinite) {
    getInput.Set();
    bool success = gotInput.WaitOne(timeOutMillisecs);
    if (success)
      return input;
    else
      throw new TimeoutException("User did not provide input within the timelimit.");
  }}

當(dāng)然,打電話很容易:

try {
  Console.WriteLine("Please enter your name within the next 5 seconds.");
  string name = Reader.ReadLine(5000);
  Console.WriteLine("Hello, {0}!", name);} catch (TimeoutException) {
  Console.WriteLine("Sorry, you waited too long.");}

或者,您可以使用TryXX(out)如Shmueli所建議的那樣:

  public static bool TryReadLine(out string line, int timeOutMillisecs = Timeout.Infinite) {
    getInput.Set();
    bool success = gotInput.WaitOne(timeOutMillisecs);
    if (success)
      line = input;
    else
      line = null;
    return success;
  }

其名稱如下:

Console.WriteLine("Please enter your name within the next 5 seconds.");string name;bool success = Reader.TryReadLine(out name, 5000);if (!success)
  Console.WriteLine("Sorry, you waited too long.");else
  Console.WriteLine("Hello, {0}!", name);

在這兩種情況下,您都不能將調(diào)用混合到Reader正常Console.ReadLine呼叫:如果Reader時(shí)間一過,就會(huì)被絞死ReadLine打電話。相反,如果你想要一個(gè)正常的(非定時(shí)的)ReadLine打電話,就用Reader并省略超時(shí),使其默認(rèn)為無限超時(shí)。

那么我提到的其他解決方案的問題呢?

  • 如您所見,使用ReadLine,避免了第一個(gè)問題。
  • 該函數(shù)在多次調(diào)用時(shí)運(yùn)行正常。不管是否發(fā)生超時(shí),只有一個(gè)后臺(tái)線程將運(yùn)行,并且最多只有一個(gè)對(duì)ReadLine的調(diào)用將是活動(dòng)的。調(diào)用函數(shù)總是會(huì)導(dǎo)致最新的輸入,或者超時(shí),用戶不必多次按Enter鍵才能提交輸入。
  • 而且,很明顯,這個(gè)函數(shù)不依賴于繁忙的等待。相反,它使用適當(dāng)?shù)亩嗑€程技術(shù)來防止資源浪費(fèi)。

我認(rèn)為這個(gè)解決方案唯一的問題是它不是線程安全的。但是,多個(gè)線程實(shí)際上不能同時(shí)請求用戶輸入,因此在調(diào)用Reader.ReadLine不管怎么說。


查看完整回答
反對(duì) 回復(fù) 2019-06-29
?
達(dá)令說

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

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();


查看完整回答
反對(duì) 回復(fù) 2019-06-29
?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

這種方法會(huì)使用Consol.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);
    }}


查看完整回答
反對(duì) 回復(fù) 2019-06-29
  • 3 回答
  • 0 關(guān)注
  • 1360 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)