等待用户输入 C# 控制台应用程序的设置时间 [英] Wait set time for user input C# console app

查看:43
本文介绍了等待用户输入 C# 控制台应用程序的设置时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于控制台应用程序,我需要知道如何在设定的时间(大约 10 秒)等待用户输入一个或一组键,然后再继续应用程序的自动运行"部分.

For a Console app, I need to know how to wait at set amount of time (about 10 seconds), for a user to input a key or set of keys, before proceeding with an 'auto run' portion of the application.

这让我很烦恼,因为我不太明白计时器是如何工作的,或者 threading.sleep,我应该使用什么?一整天都在谷歌上搜索.

This is bugging me because I can't quite figure out how the timer works, or threading.sleep, what should I use? Been googling all day.

一些伪代码:

1.应用打开

2.app 等待用户点击k"键 10 秒.

2.app waits 10 secs for user to hit the "k" key.

3.如果用户点击了k,则转到4.如果用户没有点击,则转到5.

3.if user hits k, go to 4. if user does not, go to 5.

4.运行一个函数(打开一个表单)

4.run a function(open a form)

5.运行一个函数(做某事)

5.run a function(do something)

我敢打赌这很简单,我只是不明白发生了什么.

I bet its simple, I just don't understand whats going on.

推荐答案

关闭 10 秒计时器.

Set a 10 second timer off.

在计时器到期时触发事件.

Fire an event when the timer expires.

在事件处理程序中继续自动运行"部分.

In the event handler proceed with the "auto run" section.

如果用户在计时器到期之前点击了一个键,则终止线程.

If the user hits a key before the timer expires, kill the thread.

Timer 类页面 MSDN 上有一个等待设定时间的计时器示例.

The Timer class page on MSDN has an example of a timer waiting for a set period.

这篇关于等待用户输入 C# 控制台应用程序的设置时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆