private static void ThreadingTimer()
{
string a = "OK";
System.Threading.Timer t1 = new System.Threading.Timer(TimeAction, a, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3));
}
static void TimeAction(object o)
{
Console.WriteLine("System.Threading.Timer{0:T}", DateTime.Now+o.ToString());
}
以上定时器是在2秒钟后开始执行,并且间隔3秒执行一次,
我现在想在每天凌晨4点运行一次,改如何改写??主要是那两个个时间该怎么写?谢谢了,呵呵
10 个解决方案
#1
static int hour = 0
static void TimeAction(object o)
{
if(DateTime.Now.Hour >= 4 && hour != 4)
{
hour = 4;
//................
}
if(DateTime.Now.Hour < 4 && hour == 4)
hour = 0;
}
#2
时间间隔如果想设置的长,那么在启动时要判断当前时间,然后先把时间间隔设为到下一个四点的剩余分钟,到斯点后,在TimeAction中改为24小时.
#3
if(DateTime.Now。tostring("HH")==04)
{
.....
}
{
.....
}
#4
Timer aTimer=new Timer();
aTimer.Interval = 1000;
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
aTimer.Start();
private void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (this.intHour == e.SignalTime.Hour && this.intMinute == e.SignalTime.Minute && this.intSecond == e.SignalTime.Second)
{
/*
*do your work
*/
}
}
#5
比较运行时间与当前系统时间
#6
#7
private static void ThreadingTimer()
{ string a = "OK"; System.Threading.Timer t1 = new System.Threading.Timer(TimeAction, a, TimeSpan.FromSeconds(2)/*这里如何填写当前时间跟凌晨四点的时间间隔*/, TimeSpan.FromSeconds(3));
}
static void TimeAction(object o)
{
Console.WriteLine("System.Threading.Timer{0:T}", DateTime.Now+o.ToString());
}
#8
static void Main(string[] args)
{
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
Timer tm = new Timer(CallBack, null,
(((h < 4 ? 4 : 28) - h) * 60 * 60 - m * 60 - s) * 1000 , 24 * 60 * 60 * 1000);
Console.ReadLine();
}
static void CallBack(object obj)
{
//
}
#9
如果是操作数据的话就用数据库的任务计划最稳定。
#10
感觉这样是对的,
#1
static int hour = 0
static void TimeAction(object o)
{
if(DateTime.Now.Hour >= 4 && hour != 4)
{
hour = 4;
//................
}
if(DateTime.Now.Hour < 4 && hour == 4)
hour = 0;
}
#2
时间间隔如果想设置的长,那么在启动时要判断当前时间,然后先把时间间隔设为到下一个四点的剩余分钟,到斯点后,在TimeAction中改为24小时.
#3
if(DateTime.Now。tostring("HH")==04)
{
.....
}
{
.....
}
#4
Timer aTimer=new Timer();
aTimer.Interval = 1000;
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
aTimer.Start();
private void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (this.intHour == e.SignalTime.Hour && this.intMinute == e.SignalTime.Minute && this.intSecond == e.SignalTime.Second)
{
/*
*do your work
*/
}
}
#5
比较运行时间与当前系统时间
#6
#7
private static void ThreadingTimer()
{ string a = "OK"; System.Threading.Timer t1 = new System.Threading.Timer(TimeAction, a, TimeSpan.FromSeconds(2)/*这里如何填写当前时间跟凌晨四点的时间间隔*/, TimeSpan.FromSeconds(3));
}
static void TimeAction(object o)
{
Console.WriteLine("System.Threading.Timer{0:T}", DateTime.Now+o.ToString());
}
#8
static void Main(string[] args)
{
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
Timer tm = new Timer(CallBack, null,
(((h < 4 ? 4 : 28) - h) * 60 * 60 - m * 60 - s) * 1000 , 24 * 60 * 60 * 1000);
Console.ReadLine();
}
static void CallBack(object obj)
{
//
}
#9
如果是操作数据的话就用数据库的任务计划最稳定。
#10
感觉这样是对的,