You could use a Timer. Just create a Timer and construct it with the time needed. (1000*5*60) for 5 minutes. When the time elapsed, the Timer_Elapsed
method is called. Use a boolean to switch between two methods. Reminder: The Timer_Elapsed
will be called on a different thread.
Here is an example:
using System.Timers; // <-- this timer.. Not the Windows.Forms.Timer, because that one works on the messagequeue (to receive the timer_elapsed event on the gui thread), but you don't have a messagequeue/forms
static class Program
{
private static bool _executeFirstMethod = true;
static void Main(string[] args)
{
using (Timer timer = new Timer(5000)) // 5 seconds instead of 5 minutes (for testing)
{
timer.Elapsed += Timer_Elapsed;
timer.Start();
Console.WriteLine("Timer is started");
Console.ReadLine();
}
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_executeFirstMethod)
Method1();
else
Method2();
_executeFirstMethod = !_executeFirstMethod;
}
private static void Method1()
{
Console.WriteLine("Method1 is executed at {0}", DateTime.Now);
}
private static void Method2()
{
Console.WriteLine("Method2 is executed at {0}", DateTime.Now);
}
}
Result:
Timer is started
Method1 is executed at 09-Jun-16 09:49:14
Method2 is executed at 09-Jun-16 09:49:19
Method1 is executed at 09-Jun-16 09:49:24
Method2 is executed at 09-Jun-16 09:49:29
Method1 is executed at 09-Jun-16 09:49:34
Method2 is executed at 09-Jun-16 09:49:39