3 回答

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(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)這可能不是什么大問題。
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."); }}
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)
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);
Reader
Console.ReadLine
Reader
ReadLine
ReadLine
Reader
如您所見,使用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)。
Reader.ReadLine

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

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
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); }}
- 3 回答
- 0 關(guān)注
- 1360 瀏覽
添加回答
舉報(bào)