user1765862 user1765862 - 3 months ago 11
C# Question

calling method on every x minutes

I want to call some method on every 5 minutes, how can I do this?

public class Program
{
static void Main(string[] args)
{
Console.WriteLine("*** calling MyMethod *** ");
Console.ReadLine();
}

private MyMethod()
{
Console.WriteLine("*** Method is executed at {0} ***", DateTime.Now);
Console.ReadLine();
}
}

Answer
var timer = new System.Threading.Timer((e) =>
{
    MyMethod();   
}, null, 0, TimeSpan.FromMinutes(5).TotalMilliseconds);