精确的三分钟计时器间隔

发布于 2025-01-05 22:40:29 字数 1473 浏览 1 评论 0原文

在我的上一个问题中,我询问了有关将时间值四舍五入到最接近的第三分钟的问题。 现在我的 System.Threading.Timer 遇到了一些问题,必须在第三分钟时间到来时才能工作。我执行以下操作:

private System.Timers.Timer WorkTimer;
//...
public void StartProccessing()
{
WorkTimer = new System.Timers.Timer();
WorkTimer.AutoReset = false;
WorkTimer.Elapsed += new ElapsedEventHandler(WorkTimer_Elapsed);
StartWorkTimer();
}
//...
private void StartWorkTimer()
        {
            WorkTimer.Interval = (CurrentTime.AddMinutes(3) - DateTime.Now).TotalMilliseconds;
            WorkTimer.Start();
        }
void WorkTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            WorkTimer.Stop();
            this.ProcessData(this.CurrentTime);
            StartWorkTimer();
        }

问题是计时器启动时 - 它在第一个第三分钟时间不工作,它在第二个第三分钟时间后开始工作。例如: 计时器于 15.02.2012 12:20:32.871 启动,在 15.02.2012 12:21:00.000 它必须工作 WorkTimer_Elapsed,但它从 15.02.2012 12:24:01.871 开始。 如何解决这个问题? 我修复了 CurrentTime 时间(来自上一个问题):

private DateTime CurrentTime
        {
            get
            {
                DateTime now = DateTime.Now.AddSeconds(30);
                DateTime val;
                val = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0)
                     .AddMinutes(((now.Minute) / 3) * 3 - now.Minute);
                return val;
            }
        }

In my previos question I asked about rounding time value to nearest third-minute.
Well now I have some issues with my System.Threading.Timer that must work when is third-minute time is come. I Do following:

private System.Timers.Timer WorkTimer;
//...
public void StartProccessing()
{
WorkTimer = new System.Timers.Timer();
WorkTimer.AutoReset = false;
WorkTimer.Elapsed += new ElapsedEventHandler(WorkTimer_Elapsed);
StartWorkTimer();
}
//...
private void StartWorkTimer()
        {
            WorkTimer.Interval = (CurrentTime.AddMinutes(3) - DateTime.Now).TotalMilliseconds;
            WorkTimer.Start();
        }
void WorkTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            WorkTimer.Stop();
            this.ProcessData(this.CurrentTime);
            StartWorkTimer();
        }

Problem is that the when timer started - it is not work in first third-minute time, its begin working after second third-minute time. For example:
Timer is started at 15.02.2012 12:20:32.871, in 15.02.2012 12:21:00.000 it must work WorkTimer_Elapsed, but its begin from 15.02.2012 12:24:01.871. How to fix this?
I fix my CurrentTime time (from previos question):

private DateTime CurrentTime
        {
            get
            {
                DateTime now = DateTime.Now.AddSeconds(30);
                DateTime val;
                val = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0)
                     .AddMinutes(((now.Minute) / 3) * 3 - now.Minute);
                return val;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2025-01-12 22:40:29

这里的问题是您四舍五入到最近的第 3 分钟,但为了使计时器正常工作,您必须使用最近的计时器。

因此,如果您删除 AddSeconds(30),您的计时器应该按预期工作。

旁注:您的舍入算法应添加 90 秒(因为这是 3 分钟的一半)而不是 30 秒。如果您将 30 秒添加到 12:31:50,则最终会得到 12:30:00 ,但应该改为 12:33:00。

The problem here is that you round to the nearest 3rd minute, but for your timer to work you have to use the most recent one.

So, if you remove the AddSeconds(30), your timer should work as expected.

On a side note: your rounding algorithm should add 90 seconds (as that's the half of 3 minutes) rather than 30. If you're adding 30 seconds to 12:31:50, you'd end up with 12:30:00, but it should be 12:33:00 instead.

肤浅与狂妄 2025-01-12 22:40:29

使用 System.Threading.Timer 类。

DateTime dt = ... // here you assign the start time, so you can calculate
                  // the first time you want to start it.
var timer = new Timer(callback, null, dt, TimeSpan.FromMinutes(3));

回调是:

public delegate void TimerCallback(Object state)

Use the System.Threading.Timer class.

DateTime dt = ... // here you assign the start time, so you can calculate
                  // the first time you want to start it.
var timer = new Timer(callback, null, dt, TimeSpan.FromMinutes(3));

Callback is:

public delegate void TimerCallback(Object state)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文