Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# time InputLanguage
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 }
            }
Posted 24-Dec-11 8:33am
Edited 24-Dec-11 8:36am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
idle63 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

 
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 ");
                }
            }
          
        }
 
    }
  Permalink  
v3
Comments
Abhinav S at 24-Dec-11 13:49pm
   
Right 5.
Monjurul Habib at 24-Dec-11 13:53pm
   
thank you
Monjurul Habib at 24-Dec-11 14:26pm
   
check my alternative
idle63 at 24-Dec-11 14:29pm
   
Ok will delete my comment so my 5 for your alternative
Monjurul Habib at 24-Dec-11 14:32pm
   
hay why you deleted your comment, that was good. i appreciate that..thanks for the good comment and vote ;)
idle63 at 24-Dec-11 14:39pm
   
Deleted, simply in the hope to keep the forum short. You posted a good solution so my comment is not realy needed ;)
Regards.

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

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 24 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100