【问题标题】:Calling two methods alternately after every 5 minutes.每 5 分钟后交替调用两种方法。
【发布时间】:2016-06-08 08:16:22
【问题描述】:

我想每隔 5 分钟交替调用两个方法,我该怎么做?

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();
   }
}

感谢任何帮助。 谢谢..!

【问题讨论】:

  • 你需要一个定时器msdn.microsoft.com/en-us/library/…
  • @RahulHendawe 创建一个 exe 并使用 Windows 中的任务计划程序调用它。或者在其中创建一个 Windows 服务和计时器,间隔为 5 分钟。
  • @Alex:谢谢,任何使用计时器从 Windows 服务交替调用 first 5 min call method1 next 5 min call method2 等方法的示例示例。
  • @Alex .exe 与任务计划程序将无法实现所需的结果(特别是交替方法调用)。 Windows 服务似乎有点矫枉过正,并且无法访问System.Console,或任何 UI 线程交互。 OP 只需要一个Timer

标签: c#


【解决方案1】:

您可以使用计时器。只需创建一个 Timer 并使用所需的时间构建它。 (1000*5*60) 5 分钟。当时间过去时,Timer_Elapsed 方法被调用。使用布尔值在两种方法之间切换。 提醒:Timer_Elapsed 将在不同的线程上调用。

这是一个例子:

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);
    }
}

结果:

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

【讨论】:

  • 我认为这个答案抓住了问题的本质(每 5 分钟运行一次)。
  • @Edgars:是的,我想每隔 5 分钟调用一次方法。
  • @Jeroen van Langgen :谢谢,但是 5 分钟后将 _executeFirstMethod 设置为 false 后,它会在接下来的 5 分钟后再次调用 Method1 吗?
  • 是的,_executeFirstMethod = !_executeFirstMethod; 将翻转布尔值,下一次调用它将调用另一个方法,(已测试)
【解决方案2】:

如果您的程序遵循问题中的基本结构,那么您甚至不需要计时器,只需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);
   }
}

【讨论】:

  • 使用Thread.Sleep() 比使用Timer 有什么优势吗?作为一种通用方法,我看到了很多缺点(例如,是否需要在调用之间进行任何其他检查,例如 Windows 服务重新启动/停止命令),但也许我错过了一些重要的优势?
  • @CoolBots - 我刚刚修改了他的例子来展示解决他的问题的一种方法。有很多方法可以做到这一点,不需要计时器,这只是其中一种。
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2021-04-05
  • 2015-09-08
相关资源
最近更新 更多