Help me figure out how to implement the method call in C # after 60 seconds? That is, I launched, for example, a program that should always be executed until I close it myself. Then the method is immediately executed, after its completion a wait of 60 seconds. and start the method from the beginning. Thoughts are as follows:

static void Main(string[] args) { StartProgram start = new StartProgram(); while (true) { start.Circle(); System.Threading.Thread.Sleep(60000); } } 

Or :

 TimerCallback tm = new TimerCallback(start.Circle); Timer timer = new Timer(tm, null, 0, 60000); 

Or how to turn the program in a circle?

  • 3
    If the interval between runs is equal, then the timer. If 60 seconds after the end of the previous method, then Sleep () /Task.Delay () - vitidev

1 answer 1

I think the correct option is 2.

In the first version, you stop the current stream entirely, and could perform some useful work. You can, of course, run a separate stream and do Sleep in it, but for repeating the same action at regular intervals, the Timer is more suitable than the invention of a bicycle.

In the second case, you subscribe to the event, and the events occur in a separate thread.

  • Thank you very much for the answer, but do not tell me how to implement a countdown? Well, to see how much time is left before updating the system? or, for example, by pressing 2 buttons ctrl + L, the program was executed regardless of the timer, and the timer was reset. Or do I create a new topic? - Ethernets
  • @Ethernets, a separate topic is better. - iluxa1810
  • @Ethernets, on the first point, to vskidku: Make that the timer would work every second => you can display how many seconds have passed, and inside the method analyze whether 60 seconds have passed or not. - iluxa1810
  • I started to deal with the timer, and I realized that it works every 60 seconds, for example, and what if my method has not finished yet? Everything starts on a new one, can there be a way how to stop the timer for the duration of the execution, and then start it on a new one after the method is completed? - Ethernets
  • @Ethernets, at the entrance to the method, Enable the timer to false translate, and the output to true. - iluxa1810