我该如何编写一个警报,让它每天在 00:00、00:15、00:30 等时间运行?
你能给我一个示例代码吗?
谢谢。
我该如何编写一个警报,让它每天在 00:00、00:15、00:30 等时间运行?
你能给我一个示例代码吗?
谢谢。
private void OnTimerTick()
{
if(DateTime.Now.Minutes%15==0)
{
//Do something
}
}
但如果您正在寻找一个需要每15分钟运行的程序或服务,您可以编写您的应用程序,然后使用Windows计划任务启动它。
IJob
接口紧密耦合到quartz.dll上,并且2)要求每个数据都可序列化。这里是一个简单版本:https://github.com/mjeson/SimpleJobScheduler - Jeson Martajayausing System;
using System.Threading;
using System.Windows.Forms;
using System.IO;
public class TimerExample
{
public static void Main()
{
bool jobIsEnabledA = true;
bool jobIsEnabledB = true;
bool jobIsEnabledC = true;
Console.WriteLine("Starting at: {0}", DateTime.Now.ToString("h:mm:ss"));
try
{
using (StreamWriter writer = File.AppendText("C:\\scheduler_log.txt"))
{
while (true)
{
var currentTime = DateTime.Now.ToString("h:mm");
if (currentTime == "3:15" && jobIsEnabledA)
{
jobIsEnabledA = false;
ThreadPool.QueueUserWorkItem((state) => { MessageBox.Show(string.Format("Time to brush your teeth! {0}", currentTime) ); });
}
if (currentTime == "3:20" && jobIsEnabledB)
{
jobIsEnabledB = false;
ThreadPool.QueueUserWorkItem((state) => { MessageBox.Show(string.Format("Time to brush your teeth! {0}", currentTime)); });
}
if (currentTime == "3:30" && jobIsEnabledC)
{
jobIsEnabledC = false;
ThreadPool.QueueUserWorkItem((state) => { MessageBox.Show(string.Format("Time for your favorite show! {0}", currentTime)); });
}
if (currentTime == "3:31")
{
jobIsEnabledA = true;
jobIsEnabledB = true;
jobIsEnabledC = true;
}
var logText = string.Format("{0} jobIsEnabledA: {1} jobIsEnabledB: {2} jobIsEnabledC: {3}", DateTime.Now.ToString("h:mm:ss"), jobIsEnabledA, jobIsEnabledB, jobIsEnabledC);
writer.WriteLine(logText);
Thread.Sleep(1000);
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
例如:
using System.Timers;
class Program
{
static void Main(string[] args)
{
Timer timer = new Timer();
timer.Interval = new TimeSpan(0, 15, 0).TotalMilliseconds;
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
throw new NotImplementedException();
}
}
在.NET中有不同的计时器