1

I want to call two methods alternately after every 5 minutes, how can I do this?

public class Program
{
   static void Main(string[] args)
   {
      Console.WriteLine(" calling Method1 ");
      /* After Next 5 minute call Method2 */ 
      Console.WriteLine(" calling Method2 ");
      Console.ReadLine();
   }    
   private Method1()
   {
     Console.WriteLine("Method1 is executed at {0}", DateTime.Now);
     Console.ReadLine();
   }
   private Method2()
   {
     Console.WriteLine("Method2 is executed at {0}", DateTime.Now);
     Console.ReadLine();
   }
}

Appreciate any Help. Thanks..!

Rahul Hendawe
  • 902
  • 1
  • 14
  • 39
  • 3
    You need a Timer https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx – Steve Jun 08 '16 at 08:17
  • 1
    @RahulHendawe Create an exe and call it using Task Scheduler in Windows. Or create a Windows service and timer inside of it with 5 minute intervals. – Alex Jun 08 '16 at 08:21
  • @Alex: Thanks, any sample example to call a method alternately like `first 5 min call method1 next 5 min call method2` and so on from windows service using timer. – Rahul Hendawe Jun 08 '16 at 08:29
  • @Alex .exe with task scheduler will not accomplish the desired result (specifically, alternating method calls). Windows Service seems overkill, and will not have access to `System.Console`, or any UI thread interaction. OP just needs a `Timer` – CoolBots Jun 08 '16 at 08:35

2 Answers2

3

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
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
1

If your program follows the basic structure in your question then you don't even need a timer, just Thread.Sleep:

public class Program
{
   static void Main(string[] args)
   {
      while(true)
      {
        Method1();
        System.Threading.Thread.Sleep(TimeSpan.FromMinutes(5));

        Method2();
        System.Threading.Thread.Sleep(TimeSpan.FromMinutes(5));
      }
   }    
   private Method1()
   {
     Console.WriteLine("Method1 is executed at {0}", DateTime.Now);
   }
   private Method2()
   {
     Console.WriteLine("Method2 is executed at {0}", DateTime.Now);
   }
}
Sean
  • 60,939
  • 11
  • 97
  • 136
  • Is there any advantage to using `Thread.Sleep()` over a `Timer`? I see quite a few disadvantages as a general approach (for instance, if any other checking needs to happen between calls, such as a Windows Service restart/stop commands), but perhaps I am missing some significant advantage? – CoolBots Jun 08 '16 at 19:20
  • @CoolBots - I've just adapted his example to show one way of solving his problem. There's a number of ways to do this which don't require a timer, and this is just one. – Sean Jun 08 '16 at 20:04