2

I am used to the old WCF way of timers/ticking. I want, for example, a certain method to be called every 25th second. How do I do this in .NET Core?

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

1

You can use a System.Threading.Timer. Be aware that this Timer is not thread safe - see this question for details: Timer (System.Threading) thread safety .

namespace TimerApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (new System.Threading.Timer(DoSomething, null , 0, 250))
            {
                // do something else
                Thread.Sleep(TimeSpan.FromDays(1));
            }
        }

        public static void DoSomething(object state)
        {
            // called every 250ms
            Console.WriteLine(DateTime.Now);
        }
    }
}
Community
  • 1
  • 1
Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55