타임 아웃 메커니즘을 추가합니까?
완전한 솔루션
Console.ReadLine()
예비 기능 : Console.ReadLine()
다른 방법은 를 사용하여 편집기를 포함하여 모든 기능을 유지합니다.
는 여러 스레드 또는 데드 잠금 장치를 생성하지 않고 지속적인 통화가 정상적으로 실행될 수 있도록합니다. 바쁜 대기를 제거하십시오 : 이 솔루션은 멀티 스레드를 사용하여 폐기물을 피하고 관련 자원을 기다리는 중다.
예제 사용
<code class="language-csharp">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(); } } public static string ReadLine(int timeOutMillisecs = Timeout.Infinite) { getInput.Set(); bool success = gotInput.WaitOne(timeOutMillisecs); if (success) return input; else throw new TimeoutException("用户未在规定时间内提供输入。"); } public static bool TryReadLine(out string result, int timeOutMillisecs = Timeout.Infinite) { getInput.Set(); bool success = gotInput.WaitOne(timeOutMillisecs); if (success) { result = input; return true; } else { result = null; return false; } } }</code>메소드를 사용할 수 있습니다.
결론
위 내용은 Console.Readline ()에 타임 아웃을 추가하려면 어떻게해야합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!