如何使用c#在循环中每10分钟运行一个函数



我有一个循环,它根据某些条件连续运行函数。现在,我只想在循环中每10分钟调用一次该函数。我使用的是Visual Studio 2005。我的代码是:

    while (boolValue == false)
    {
         Application.DoEvents();
         StartAction();    //i want to call this function for every 10 minutes only
    }

我正在使用System.Timers,但它没有调用函数。我不知道怎么了。

我的代码是:

   public static System.Timers.Timer aTimer;
   while (boolValue == false)
   {
        aTimer = new System.Timers.Timer(50000);
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
   }
   private static void OnTimedEvent(object source, ElapsedEventArgs e)
   {
        Application.DoEvents();
        StartAction();
   }

为什么不使用计时器呢。每十分钟触发一次。

更具体版本中的例子是一个很好的例子,实际上是

更新

在您更新的代码中,我会将其更改为:

public static System.Timers.Timer aTimer;
...
aTimer = new System.Timers.Timer(50000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = false; //This should be true if you want it actually looping
aTimer.Enabled = true;

我看不出有什么理由有一段时间的循环。我的猜测是while循环根本没有被触发。此外,您可能应该将"自动重置"设置为true,这样它才能连续运行。

最新更新