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

Asked

Viewed 1,212 times

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();
   }
}
  • 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).

2 answers

2

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.

1

Okay, 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, that is, 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 run the TimerCallback in your example, will run whenever you are.

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

  • I think there are some things wrong there, or are you a little confused.

  • Yes, I used Timercallback only once, when I wrote, I was in doubt in some things, but I wrote the same way to try to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.