How to use and what is the timer function in C #?

0
//Exemplo de uso(Que não entendi):
using System;
using System.Threading;

public static class Program {

   public static void Main() {
      // Create a Timer object that knows to call our TimerCallback
      // method once every 2000 milliseconds.
      Timer t = new Timer(TimerCallback, null, 0, 2000);
      // Wait for the user to hit <Enter>
      Console.ReadLine();
   }

   private static void TimerCallback(Object o) {
      // Display the date/time when this method got called.
      Console.WriteLine("In TimerCallback: " + DateTime.Now);
      // Force a garbage collection to occur for this demo.
      GC.Collect();
   }
}
    
asked by anonymous 10.06.2017 / 13:52

2 answers

2

Timer is not a function. It is a class that provides a mechanism that allows specifying a frequency of time that a given operation should be executed.

This is how the object is created by configuring what should be executed through a delegate and when executing ( TimerCallback ).

You can pass an object so that this delegate can process, but in the posted example it passes a null, since nothing is required ( null ). It is of type object to give freedom to pass anything. This is between your code that sets Timer , in this case Main() , and the method that will work as a callback , in this case TimerCallback()

It is also a time that should take to run the first time, with 0 being the immediate start, which is what was passed.

And the interval between each execution is passed, in the last 2000 case it 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 effective with several overloads of builders .

Always look for official documentation for learn about a class or method of it. It always has the explanation of what it does, which means every part of it and usually has an example of use and some care.

Understand what a delegate is an group of methods and learn why it only passes the method name and nothing else. One more question on the subject .

See 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 delegate method calls the garbage collector. This should not be done under normal conditions . If it is a course that teaches this, consider a red alert for the content, unless there is a context and explanation why there is.

    
10.06.2017 / 14:21
0

Ok, let's go to your sample code.

Timer t = new Timer(TimerCallback, null, 0, 2000);

The first parameter (TimerCallback) you are calling the function private static void TimerCallback(Object o) to start whenever the timer hits the 2 seconds, ie it will run every 2 seconds (following in your example).

The second parameter (null) is the state of your timer, in your case the timer will never stop because it is null.

The third parameter, is when the timer will execute the TimerCallback in its example, will execute whenever it is 0ms.

The fourth parameter is the time interval of the timer, in its example (2000ms) , it will run every 2 seconds. In other words, it will start from 0 and when it gives 2000ms again, it will return to 0.

    
10.06.2017 / 14:17