views:

59

answers:

2

I have a schedule checker that needs to run until a user presses a key to make it stop.

My thoughts on doing this are as follows:

  1. The user starts the process
  2. Process runs every 10 seconds
  3. When the process is not running, Console.ReadLine(); gives the user the option to press "q" to stop the process

My approach would work fine except the user would have to wait however long the process takes to complete before being able to stop it. Is there any way to simultaneously run the process while being able to take a user input?

+2  A: 

You could run the process that does the schedule work on a separate thread. Then the main thread could always be responsive to the user input. On the other hand, if you cannot kill the other process while it is running, the end result would not be any different. The entire application would not be able to stop until that process completed. If, though, you can kill the other process at any time, then multiple (two) threads would probably work well.

Mark Wilkins
Mark, this is great. I will look further into this topic of multi-threading and hopefully build a usable solution.
Soo
+2  A: 

Maybe a BackgroundWorker can help.

Here is an example, that uses also cancelation.
There is also one at MSDN.

Oliver