[英]VB.NET 2010 Console Application Wait for a key for 5 seconds, carry on if not
Ok, so I've tried looking up on several Google sites and cannot find an answer for this. 好的,所以我尝试在多个Google网站上查找,但找不到答案。
Its in VB.Net as a Console Application, the way I want to set this up is almost like BIOS entry, when you turn your computer on you have several seconds to press a key, if its not pressed it will skip on, this is easy enough in a Windows Forms application with a timer, but I'm struggling to do it in Console application. 在VB.Net中作为控制台应用程序,我要进行设置的方式几乎类似于BIOS条目,当您打开计算机时,您有几秒钟的时间可以按一个键,如果不按它会跳过,这是在Windows窗体应用程序中使用计时器很容易,但是我在控制台应用程序中很难做到。
code so far: 到目前为止的代码:
Console.WriteLine("Welcome to console, press F2 for advanced options.")
''Console.ReadKey(keys.f2)
''Make it pause for 5 seconds, tried obvious threading,thread.sleep(5000) - that halts the application :/
So yeah, if you understand what I mean and are able to help out, anything is appreciated. 是的,如果您理解我的意思并能够提供帮助,那么一切都会感激不尽。 There doesn't seem to be any obvious timer function.
似乎没有任何明显的计时器功能。 I was thinking of having a variable that slowly increments to 100, and uses thread.sleep, but I think its chance of picking up the key is low because .sleep halts the application.
我本来想让一个变量逐渐增加到100,并使用thread.sleep,但我认为它选择键的机会很低,因为.sleep会暂停应用程序。 Was also thinking of console.readkey, but unsure how to read a specific key during the wait process.
还考虑过console.readkey,但是不确定如何在等待过程中读取特定的键。
Using a Do loop, Console.KeyAvailable, and a stopwatch 使用Do循环,Console.KeyAvailable和秒表
Console.WriteLine("Welcome to console, press F2 for advanced options.")
Dim stpw As Stopwatch = Stopwatch.StartNew
Dim waitfor As New TimeSpan(0, 0, 5)
Dim kp As ConsoleKeyInfo
Do
If Console.KeyAvailable Then
kp = Console.ReadKey(True)
If kp.Key = ConsoleKey.F2 Then
Exit Do
End If
End If
Threading.Thread.Sleep(100)
Loop While stpw.Elapsed < waitfor
If kp.Key = ConsoleKey.F2 Then
Console.WriteLine("F2 press")
Else
Console.WriteLine("NO F2")
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.