views:

394

answers:

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.

+2  A: 

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
OK. I THINK THIS MAY HAVE HELPED! MY BRAIN
Marlon
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
@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
Ok, I did that. Theres no code example, but I'm marking this as the answer. Thanks!
Marlon
A: 

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);
gbogumil
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
yes. the question is much clearer now. my answer is not a solution for the edited question.
gbogumil
+1  A: 

Possible answers here if you're using a Console application.

Jesse C. Slicer
yo i'm so stupid. I do not understand how the code you linked to works :(. it looks like it might help tho. could you explain it to me?
Marlon
Basically - the issue is that Microsoft's Console will block forever even if you initiate it on a thread and then kill that thread - the Console is per-process. So what my code does is actually have 2 executables: the first is a simple readline which echoes back to the console and the other calls that one on a thread which times out after a given number of seconds. If the timeout is reached before the client enters something, that app is terminated and a default (if given) string is returned. Does that help?
Jesse C. Slicer
+1  A: 

Hi Marlon,

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();
        }
I ended up using the timer, but this looks great. I up-voted it.
Marlon