C # Looping a process and until the user presses a key

I have a schedule checker program that should run until the user presses a key to stop it.

My thoughts on this are as follows:

  • The user starts the process.
  • The process starts every 10 seconds.
  • When the process is not running, Console.ReadLine (); gives the user the ability to press q to stop the process.

My approach will work perfectly, except that the user will have to wait until the process is completed before he can stop it. Is there a way to start the process at the same time while entering user input?

+3
source share
2 answers

You can start a process that runs a schedule in a separate thread. Then the main thread can always respond to user input. On the other hand, if you cannot kill another process while it is running, the end result will not be different. The entire application will not be able to stop until this process is complete. If, however, you can kill another process at any time, then several (two) threads will probably work well.

+3
source

Perhaps BackgroundWorker might help .

Here is an example that also uses cancellation.
There is also an MSDN .

+3
source

Source: https://habr.com/ru/post/1746197/


All Articles