4

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.

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.

some psuedocode:

1.app opens

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

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

4.run a function(open a form)

5.run a function(do something)

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

Marlon
  • 127
  • 1
  • 4
  • 12
  • What kind of program? WinForms? – SLaks Apr 26 '10 at 15:32
  • Is this a console application or a win-forms application? –  Apr 26 '10 at 15:33
  • Please explain a little more information. What did you mean by the "Auto Run"? can you give some examples about how the app should behave? – Shoban Apr 26 '10 at 15:40
  • 2
    Strange UI design. What happens if the user sneezes and needs 11 seconds to complete the input? – Hans Passant Apr 26 '10 at 16:22
  • its a console app. sorry let me change my question. thx for looking. Also, by auto run, i mean automatically continue the application process. – Marlon Apr 26 '10 at 17:42
  • I need it to be able to ran in an automatic mode, or configured if ran by a user. just like when you boot windows and you have like 2 seconds to hit f8 or something... – Marlon Apr 26 '10 at 17:58

4 Answers4

11

Here's some sample code for a C# console application. It doesn't use a timer, instead it uses Sleep. It may be a bit easier to understand than timer based code.

        static void openForm()
        {
            Console.WriteLine("Form opened!");
        }

        static void doSomething()
        {
            Console.WriteLine("Do something!");
        }

        static void Main(string[] args)
        {
            bool optionForm = false;
            int seconds = 1;

            Console.Write("Press 'k' to open form");

            while (true)
            {                
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo c = Console.ReadKey(true);
                    if (c.Key == ConsoleKey.K)
                    {                        
                        optionForm = true;
                        break;
                    }
                }

                System.Threading.Thread.Sleep(1000);

                if (seconds++ > 10)
                    break;

                Console.Write('.');
            }

            Console.WriteLine();

            if (optionForm)
                openForm();
            else
                doSomething();

            Console.ReadKey();
        }
4

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.

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

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • OK. I THINK THIS MAY HAVE HELPED! MY BRAIN – Marlon Apr 26 '10 at 17:59
  • OK NOW I NEED THE PORTION WHERE I READ THE INPUT KEY DURING THE TIMER. I HAVE A WHILE(KEY IS NOT EQUAL TO 'K') LOOP. should I have this? I have like, programming constipation right now.. – Marlon Apr 26 '10 at 18:04
  • @Marlon - That would work but isn't necessary. Have a key down handler and in that stop the thread if the user presses "K". – ChrisF Apr 26 '10 at 19:33
  • Ok, I did that. Theres no code example, but I'm marking this as the answer. Thanks! – Marlon Apr 29 '10 at 13:20
1

Thanks Marlon!! It really helped me a lot..

I used following code:

int minutes = 1;
while (true)
{
   if (Console.KeyAvailable)
   {
        ConsoleKeyInfo c = Console.ReadKey(true);
    if (c.Key == ConsoleKey.Enter)
    {
            break;
    }
   }
   Thread.Sleep(1000);
   if (minutes++ > 10)
   {
    throw;
   }
}
Cosmin
  • 21,216
  • 5
  • 45
  • 60
Kautik
  • 11
  • 1
  • It's not minutes but seconds, and I've never seen a sole `throw` outside a `catch`. – mafu Sep 10 '20 at 13:45
0

Here is some code that will do the trick for you too.

        Form1 f = new Form1();
        System.Threading.Timer t = new System.Threading.Timer(o => f.Invoke(new Action(() => f.textBox1.Enabled = true)), null, 10000, System.Threading.Timeout.Infinite);
        f.ShowDialog();
        t.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
Greg Bogumil
  • 1,903
  • 15
  • 23
  • Thanks for your answer. but I don't see where you judge the input of the user, in the time allotted. Maybe I should change my question a bit. :( – Marlon Apr 26 '10 at 17:52
  • yes. the question is much clearer now. my answer is not a solution for the edited question. – Greg Bogumil Apr 27 '10 at 15:12