The point is this: there is a method that should be performed frequently on a timer, but the execution of the method can be delayed (for example, interval = 100 ms, runtime = 2-3 s). What timer will behave? In particular, I need to exclude parallel execution of the method.

I tried to test it manually with System.Timers.Timer - it turns out something strange: the method seems to run in parallel, but less than it should (i.e. not every 100 ms). For System.Threading.Timer result should be the same, so I understand? System.Windows.Forms.Timer so far did not touch.

Please explain how it works? Or where you can read on the topic?

  • Immediately an important question: in which thread should the method work? Since the method is long, it cannot be a UI thread, right? For the same reason, you do not have the right to block the stream in which the alert comes from the timer (however this stream is). - VladD
  • I read your answer and realized that I do not know anything) UI-stream is a stream in which the forms are drawn, as I understand it? And run the method in this thread is bad, because the forms will hang, right? So you need to create a separate thread and start the timer from there? In general, I thought and realized that I don’t really need a timer — just a separate thread that will call the method in a loop and sleep if necessary. - djebedia
  • @djebedia: (1) yes, this is the stream in which the UI is drawn. Running slow functions in this thread is bad, and the truth will hang. (2) See how timers work. System.Windows.Forms.Timer delivers event's to the UI stream, since this is usually what is required (once) and you can send your code there because of the event loop (two). System.Timers.Timer cannot do this, because it does not know at all that there is some kind of UI-stream (it does not depend on the presence of UI), and therefore delivers event'y to its own stream. If you perform a long method in this stream, you block timely delivery ... - VladD
  • @djebedia: ... subsequent events in this stream, since the stream cannot simultaneously execute your method and process the event. It also seems to me that a good solution would be to unload the “long” task into a separate, dedicated stream. - VladD
  • I agree, for these purposes you can use the Parallel class or Task - Alexsandr Ter

0