在c#中每x秒执行一次操作y分钟

时间:2021-03-31 01:08:51

I need to run a function every 5 seconds for 10 minutes.

我需要每5秒运行一次功能10分钟。

I use a timer to run it for 5 secs, but how do I limit the timer to only 10 mins?

我使用计时器运行它5秒钟,但如何将计时器限制为仅10分钟?

9 个解决方案

#1


Just capture the time that you want to stop and end your timer from within the elapsed handler. Here's an example (note: I used a System.Threading.Timer timer. Select the appropriate timer for what you are doing. For example, you might be after a System.Windows.Forms.Timer if you are writing in Winforms.)

只需捕获您想要停止的时间并从已用过的处理程序中结束计时器。这是一个例子(注意:我使用了System.Threading.Timer计时器。为你正在做的事情选择合适的计时器。例如,如果你在Winforms中写作,你可能会在System.Windows.Forms.Timer之后。)

public class MyClass
{
    System.Threading.Timer Timer;
    System.DateTime StopTime;
    public void Run()
    {
        StopTime = System.DateTime.Now.AddMinutes(10);
        Timer = new System.Threading.Timer(TimerCallback, null, 0, 5000);
    }

    private void TimerCallback(object state)
    {
        if(System.DateTime.Now >= StopTime)
        {
            Timer.Dispose();
            return;
        }
        // Do your work...
    }
}

#2


Have your timer loop something like this:

你的计时器循环如下:

DateTime endTime = DateTime.Now.AddMinutes(10);

while(endTime < DateTime.Now)
{
  // Process loop
}

#3


Divide the Y minutes by the X interval to get how many times it needs to run. After that you just need to count how many times the function has been called.

将Y分钟除以X间隔以获得需要运行的次数。之后,您只需要计算调用函数的次数。

In your case, 10 min = 600 seconds / 5 seconds = 120 calls needed. Just have a counter keep track of how many times your function has been called.

在您的情况下,10分钟= 600秒/ 5秒=需要120次通话。只需让计数器跟踪你的函数被调用了多少次。

#4


Timer.Stop() after 120 Ticks.

120Tick之后的Timer.Stop()。

#5


just use a DateTime variable to track when it should end and set that right before you start. The on your Elapsed event handler, check if the signal time is less than the end time. If it isn't, stop the timer.

只需使用DateTime变量来跟踪它应该何时结束并在开始之前设置它。在您的Elapsed事件处理程序上,检查信号时间是否小于结束时间。如果不是,请停止计时器。

#6


You can calculate how times your function will be call, and create decrement counter, after elapsed which you unsubscribe from timer tick. Or you can Run another timer which have tick period - 10 min and on tick you unsubscribe from timer tick calling your function.

您可以计算函数调用的次数,并创建递减计数器,经过一段时间后取消订阅计时器滴答。或者你可以运行另一个有嘀嗒周期的计时器--10分钟,然后在你打开你的功能时取消订阅计时器。

#7


Note the start time. In each call, test if currentTime + 5 seconds > startTime + 10 minutes. If so, disable the timer.

注意开始时间。在每次调用中,测试currentTime + 5秒> startTime + 10分钟。如果是,请禁用计时器。

I prefer this approach to just running for N ticks, as timers are not guaranteed to fire when you'd like them to. It's possible 120 ticks may run over 10 minutes of real world time.

我更喜欢这种方法只运行N刻度,因为定时器不能保证在你喜欢的时候触发。现有世界时间超过10分钟可能会有120个刻度。

#8


You can set two timers one that run for 5 secs and the other one that run for 10min and disable the first one

您可以设置两个运行5秒的计时器,另一个运行10分钟并禁用第一个计时器

#9


You could use a second timer:

你可以使用第二个计时器:

class Program
{
    static void Main(string[] args)
    {
        int interval = 5 * 1000; //milliseconds
        int duration = 10 * 60 * 1000; //milliseconds

        intervalTimer = new System.Timers.Timer(interval);
        durationTimer = new System.Timers.Timer(duration);

        intervalTimer.Elapsed += new System.Timers.ElapsedEventHandler(intervalTimer_Elapsed);
        durationTimer.Elapsed += new System.Timers.ElapsedEventHandler(durationTimer_Elapsed);

        intervalTimer.Start();
        durationTimer.Start();
    }

    static void durationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        intervalTimer.Stop();
        durationTimer.Stop();
    }

    static void intervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //call your method
    }

    private static System.Timers.Timer intervalTimer;
    private static System.Timers.Timer durationTimer;
}

#1


Just capture the time that you want to stop and end your timer from within the elapsed handler. Here's an example (note: I used a System.Threading.Timer timer. Select the appropriate timer for what you are doing. For example, you might be after a System.Windows.Forms.Timer if you are writing in Winforms.)

只需捕获您想要停止的时间并从已用过的处理程序中结束计时器。这是一个例子(注意:我使用了System.Threading.Timer计时器。为你正在做的事情选择合适的计时器。例如,如果你在Winforms中写作,你可能会在System.Windows.Forms.Timer之后。)

public class MyClass
{
    System.Threading.Timer Timer;
    System.DateTime StopTime;
    public void Run()
    {
        StopTime = System.DateTime.Now.AddMinutes(10);
        Timer = new System.Threading.Timer(TimerCallback, null, 0, 5000);
    }

    private void TimerCallback(object state)
    {
        if(System.DateTime.Now >= StopTime)
        {
            Timer.Dispose();
            return;
        }
        // Do your work...
    }
}

#2


Have your timer loop something like this:

你的计时器循环如下:

DateTime endTime = DateTime.Now.AddMinutes(10);

while(endTime < DateTime.Now)
{
  // Process loop
}

#3


Divide the Y minutes by the X interval to get how many times it needs to run. After that you just need to count how many times the function has been called.

将Y分钟除以X间隔以获得需要运行的次数。之后,您只需要计算调用函数的次数。

In your case, 10 min = 600 seconds / 5 seconds = 120 calls needed. Just have a counter keep track of how many times your function has been called.

在您的情况下,10分钟= 600秒/ 5秒=需要120次通话。只需让计数器跟踪你的函数被调用了多少次。

#4


Timer.Stop() after 120 Ticks.

120Tick之后的Timer.Stop()。

#5


just use a DateTime variable to track when it should end and set that right before you start. The on your Elapsed event handler, check if the signal time is less than the end time. If it isn't, stop the timer.

只需使用DateTime变量来跟踪它应该何时结束并在开始之前设置它。在您的Elapsed事件处理程序上,检查信号时间是否小于结束时间。如果不是,请停止计时器。

#6


You can calculate how times your function will be call, and create decrement counter, after elapsed which you unsubscribe from timer tick. Or you can Run another timer which have tick period - 10 min and on tick you unsubscribe from timer tick calling your function.

您可以计算函数调用的次数,并创建递减计数器,经过一段时间后取消订阅计时器滴答。或者你可以运行另一个有嘀嗒周期的计时器--10分钟,然后在你打开你的功能时取消订阅计时器。

#7


Note the start time. In each call, test if currentTime + 5 seconds > startTime + 10 minutes. If so, disable the timer.

注意开始时间。在每次调用中,测试currentTime + 5秒> startTime + 10分钟。如果是,请禁用计时器。

I prefer this approach to just running for N ticks, as timers are not guaranteed to fire when you'd like them to. It's possible 120 ticks may run over 10 minutes of real world time.

我更喜欢这种方法只运行N刻度,因为定时器不能保证在你喜欢的时候触发。现有世界时间超过10分钟可能会有120个刻度。

#8


You can set two timers one that run for 5 secs and the other one that run for 10min and disable the first one

您可以设置两个运行5秒的计时器,另一个运行10分钟并禁用第一个计时器

#9


You could use a second timer:

你可以使用第二个计时器:

class Program
{
    static void Main(string[] args)
    {
        int interval = 5 * 1000; //milliseconds
        int duration = 10 * 60 * 1000; //milliseconds

        intervalTimer = new System.Timers.Timer(interval);
        durationTimer = new System.Timers.Timer(duration);

        intervalTimer.Elapsed += new System.Timers.ElapsedEventHandler(intervalTimer_Elapsed);
        durationTimer.Elapsed += new System.Timers.ElapsedEventHandler(durationTimer_Elapsed);

        intervalTimer.Start();
        durationTimer.Start();
    }

    static void durationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        intervalTimer.Stop();
        durationTimer.Stop();
    }

    static void intervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //call your method
    }

    private static System.Timers.Timer intervalTimer;
    private static System.Timers.Timer durationTimer;
}