Timer
is not a function, is a class that provides a mechanism that allows specifying a time frequency that a given operation must be performed.
This creates the object by configuring what should be executed through a delegate and when to execute (TimerCallback
).
You can pass an object so that this delegate can process, but in the example posted you pass a null, since nothing is necessary (null
). Is he the type object
to give freedom to pass anything. This is between your code that configures the Timer
, in case the Main()
, and the method that will function as a callback, in case the TimerCallback()
There is also a time that must take to execute the first time, with 0 being the immediate start, which is what was passed.
And the interval between each execution is passed, in the case 2000 past is 2 seconds since the measure is in milliseconds.
In the same class there are other ways to configure differently depending on the convenience of the moment. This is effect with several overloads of builders.
Always look for the official documentation to learn about a class or method of it. It always has the explanation of what it does, what it means each part of it and it usually has an example of use and some care.
Understand what a delegate and a group of methods and learn because only passes the name of the method and nothing else. One more question on the subject.
Behold what is callback, the focus is another language, but the technique is the same.
This is not the case, but there are other classes of Timer
with a slightly different mechanism.
The delegated method potentially calls the garbage collector. This shall not be done under normal conditions. If it is a course that teaches this, consider a red alert of the content, unless there is a context and explanation why this exists.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero