i am trying to achieve two different methods to start execution at the same time with different time intervals. method1() to be executed every 5 minutes and method2() to be executed every 10 minutes. i am using console application to achieve this.
public static Int64 counter = 0;
static void Main(string[] args)
{
bool tryAgain = true;
while (tryAgain)
{
try
{
Thread thread1 = new Thread(new ThreadStart(TimerMethod1));
Thread thread2 = new Thread(new ThreadStart(TimerMethod2));
thread1.Start();
thread2.Start();
}
catch(Exception ex)
{
Thread thread1 = new Thread(new ThreadStart(TimerMethod1));
Thread thread2 = new Thread(new ThreadStart(TimerMethod2));
thread1.Start();
thread2.Start();
Console.WriteLine("Error ==>" + ex.ToString());
}
}
}
public static void method1(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("method1 --" + System.DateTime.Now.ToString() + "=====>" + counter.ToString());
counter++;
}
public static void method2(object sender, ElapsedEventArgs e)
{
Console.WriteLine("method2 --" + System.DateTime.Now.ToString());
}
public static void TimerMethod1()
{
System.Timers.Timer t = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds); //execute every 5 minutes
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(method1);
t.Start();
}
public static void TimerMethod2()
{
System.Timers.Timer t = new System.Timers.Timer(TimeSpan.FromMinutes(10).TotalMilliseconds); //execute every 10 minutes
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(method2);
t.Start();
}
i am using two different timers to manage each method. this puts execution into loop without following timer. counter is just to check number of occurrence for particular method.
note: i just want to start execution of both methods at same time. both should work independently and not depend on each other once started.