{"id":419,"date":"2011-07-20T11:09:14","date_gmt":"2011-07-20T09:09:14","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=419"},"modified":"2011-07-20T11:09:14","modified_gmt":"2011-07-20T09:09:14","slug":"console-readkey-with-timeout","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/07\/console-readkey-with-timeout\/","title":{"rendered":"Console.ReadKey with timeout"},"content":{"rendered":"<p>There have been multiple times I wished I had a method for this. Often when you are testing things using a plain console application you want to view some output but not have to wait forever (using something like Thread.Sleep) or every time needing to press the &#8216;Any&#8217; key \ud83d\ude09<\/p>\n<p>Actually I&#8217;m a bit surprised .Net hasn&#8217;t got something like this built in yet but since it doesn&#8217;t I had to create one myself. I created a utility class with a single method to accomplish this. I thought about using an extension method but since the Console class is itself static extension methods does not work for it.<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public static class<\/span> ConsoleHelper<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">public static ConsoleKeyInfo<\/span> ReadKeyWithTimeOut(<span style=\"color: #0000ff;\">int<\/span> timeOutMS)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">DateTime<\/span> timeoutvalue = <span style=\"color: #0000ff;\">DateTime<\/span>.Now.AddMilliseconds(timeOutMS);<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">while<\/span> (DateTime.Now &lt; timeoutvalue)<br \/>\n{<\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #0000ff;\">if<\/span> (<span style=\"color: #0000ff;\">Console<\/span>.KeyAvailable)<br \/>\n{<\/p>\n<p style=\"padding-left: 120px;\"><span style=\"color: #0000ff;\">ConsoleKeyInfo<\/span> cki = <span style=\"color: #0000ff;\">Console<\/span>.ReadKey();<br \/>\n<span style=\"color: #0000ff;\">return<\/span> cki;<\/p>\n<p style=\"padding-left: 90px;\">}<br \/>\n<span style=\"color: #0000ff;\">else<\/span><br \/>\n{<\/p>\n<p style=\"padding-left: 120px;\">System.Threading.<span style=\"color: #0000ff;\">Thread<\/span>.Sleep(100);<\/p>\n<p style=\"padding-left: 90px;\">}<\/p>\n<p style=\"padding-left: 60px;\">}<br \/>\n<span style=\"color: #0000ff;\">return new ConsoleKeyInfo<\/span>(&#8216; &#8216;, <span style=\"color: #0000ff;\">ConsoleKey<\/span>.Spacebar, <span style=\"color: #0000ff;\">false<\/span>, <span style=\"color: #0000ff;\">false<\/span>, <span style=\"color: #0000ff;\">false<\/span>);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>What this gives you is a method that waits until the specified time-out expires or any (normal) key has been pressed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There have been multiple times I wished I had a method for this. Often when you are testing things using a plain console application you want to view some output but not have to wait forever (using something like Thread.Sleep) &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/07\/console-readkey-with-timeout\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,56,303],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-console","tag-development"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=419"}],"version-history":[{"count":4,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/419\/revisions\/423"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}