gpt4 book ai didi

C#例程准时触发

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:27 27 4
gpt4 key购买 nike

我有一个名为 nud 的 numericUpDown。nud=0.5,最小 0.5,最大 6,递增 0.5。这个 nud 是我的例程要执行的时间间隔。如果 nud==0.5,我的例程每 30 分钟运行一次;如果 nud==1,每 1 小时运行一次; nud==1.5,每1小时30分钟(90分钟)运行一次,以此类推。但是,如果 nud nud==0.5||1.5||2.5||3.5||4.5||5.5,当系统时钟像这样 HH:00:00 或 HH:30:00 时应该触发例程。(每半小时)

如果 nud==1||2||3||4||5||6,当系统时钟像这样 HH:00:00(整点)时应该触发例程。

欢迎任何提示、想法和链接。提前致谢,出现率

最佳答案

我~想~你想要这样的东西:

public partial class Form1 : Form
{

private DateTime dt;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SetInitialTimer();
}

private void SetInitialTimer()
{
// Set "dt" to the BEGINNING of the CURRENT hour:
dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);

switch (nud.Value.ToString())
{
case "0.5":
case "1.5":
case "2.5":
case "3.5":
case "4.5":
case "5.5":
// start at the NEXT 1/2 hour or top of the hour:
if (DateTime.Now.Minute < 30)
{
dt = dt.AddMinutes(30);
}
else
{
dt = dt.AddHours(1);
}
break;

default: // "1", "2", "3", "4", "5", "6"
// start at the TOP of the NEXT hour:
dt = dt.AddHours(1);
break;

}

timer1.Interval = (int)dt.Subtract(DateTime.Now).TotalMilliseconds;
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();

// ... do something in here ...

SetRecurringTimer();
}

private void SetRecurringTimer()
{
dt = dt.AddMinutes((double)(nud.Value * (decimal)60));
timer1.Interval = (int)dt.Subtract(DateTime.Now).TotalMilliseconds;
timer1.Start();
}

}

关于C#例程准时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com