C#控制台计时器/input [英] C# Console timer /input

查看:217
本文介绍了C#控制台计时器/input的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以要求用户提供输入,但是在一定时间后,如果用户不提供输入,请做其他一些事情
例如:

Is there a way to ask user for input but after a certain time do somethin else if they don''t give input
for example:

while (true)
{
    if (d.Key == ConsoleKey.UpArrow)
    {
        v--;
    }
    if (d.Key == ConsoleKey.DownArrow)
    {
        v++;
    }
    if (d.Key == ConsoleKey.RightArrow)
    {
        h++;
    }
    if (d.Key == ConsoleKey.LeftArrow)
    {
        h--;
    }
  -------- if no input after 1 second
    { do something }
}

推荐答案

创建一个线程,该线程每X秒检​​查一次键盘活动.如果线程未检测到活动,请执行某些操作.当用户拿到钥匙时,请设置一个指示活动日期时间的值.这是线程将检查以查看自上次活动以来已运行了多长时间的变量.

继续并进行编码.
Create a thread that checks for keyboard activity every X seconds. If the thread doesn''t detect activity, do something. When the user kits a key, set a value indicating the datetime of the activity. This is the variable that the thread would check to see how long it''s been since the last activity.

Go forth, and code.


while (true)
  {
    if (d.Key == ConsoleKey.UpArrow)
    {
       v--;
    }
    else if (d.Key == ConsoleKey.DownArrow)
    {
       v++;
    }
    else if (d.Key == ConsoleKey.RightArrow)
    {
       h++;
    }
    else if (d.Key == ConsoleKey.LeftArrow)
    {
       h--;
    }
    else
    {
      //Starting a timer
      Timer tmr = new Timer(ComputeBoundOp, 5, 0, 2000); 
      Thread.Sleep(10000); // //Executing other works over here (10 seconds)
      tmr.Dispose(); // Cancel the timer now

    }
}


替代


ALTERNATIVE

public class StopWatch
{
    private DateTime startTime;
    private DateTime stopTime;
    private bool running = false;
    public void Start()
    {
        this.startTime = DateTime.Now;
        this.running = true;
    }
    public void Stop()
    {
        this.stopTime = DateTime.Now;
        this.running = false;
    }
    // elaspsed time in seconds
    public double GetElapsedTimeSecs()
    {
        TimeSpan interval;

        if (running)
            interval = DateTime.Now - startTime;
        else
            interval = stopTime - startTime;

        return interval.TotalSeconds;
    }
    // sample usage
    public static void Main(String[] args)
    {
        StopWatch s = new StopWatch();
        s.Start();
        ConsoleKeyInfo d = Console.ReadKey();
        while ("CONDITION HERE")
        {
            if (d.Key == ConsoleKey.UpArrow)
            {
                Console.WriteLine("key pressed ");
            }
            if (d.Key == ConsoleKey.DownArrow)
            {
                Console.WriteLine("key pressed ");
            }
            if (d.Key == ConsoleKey.RightArrow)
            {
                Console.WriteLine("key pressed ");
            }
            if (d.Key == ConsoleKey.LeftArrow)
            {
                Console.WriteLine("key pressed ");
            }
            if (s.GetElapsedTimeSecs() < 1)
            {
                s.Stop();
                Console.WriteLine("Do something else ");
            }
        }

    }

}


这篇关于C#控制台计时器/input的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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