Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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 }
}
Posted
Updated 24-Dec-11 7:36am
v2

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.
 
Share this answer
 
Comments
[no name] 24-Dec-11 13:55pm    
5 from a cp noob. Even I do not agree with your solution to the end. The thread should signal timeout and nobody has to scan/check.

Regards
C#
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

C#
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 ");
            }
        }

    }

}
 
Share this answer
 
v3
Comments
Abhinav S 24-Dec-11 13:49pm    
Right 5.
Monjurul Habib 24-Dec-11 13:53pm    
thank you
Monjurul Habib 24-Dec-11 14:26pm    
check my alternative
[no name] 24-Dec-11 14:29pm    
Ok will delete my comment so my 5 for your alternative
Monjurul Habib 24-Dec-11 14:32pm    
hay why you deleted your comment, that was good. i appreciate that..thanks for the good comment and vote ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900