有关定时服务的问题

linfeng0810 2009-06-24 09:12:55
小弟想实现 一天内指定时间 进行某个操作,不是每隔断时间进行操作。
比如说 每天早 8点 蹦个对话框之类的,最好结合代码,谢谢
...全文
51 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
newdigitime 2009-06-24
  • 打赏
  • 举报
回复
mark,学习一下.
hwj1107 2009-06-24
  • 打赏
  • 举报
回复
在程序打开的时候,首先判断现在的系统时间跟你的8点有多少秒,换算成毫秒,赋值给时间控件的Interval
然后在时间空间的Tick里面再次将Interval的值改为24小时,记得要换算成毫秒
只要系统不关闭,你所指定的那个事件就会在每天的8点运行,而且这种不需要每秒都判断是否为指定的8点,只是利用电脑系统的时间,程序不需要做这个每秒都判断的工作
lovvver 2009-06-24
  • 打赏
  • 举报
回复
Timer timer = new Timer();
timer.Interval = 60 * 1000;
timer.Elapsed += timer_Elapsed;
timer.Start();

private void timer_Elapsed(object sender,ElapsedEventArgs e)
{
if(DateTime.Now.Hour = 5 && DateTime.Now.Minute = 0)
{
MessageBox.Show("");
}
}

楼主最好每分钟检测一次,这个对效率不会有多少影响。
linfeng0810 2009-06-24
  • 打赏
  • 举报
回复
有点类似于 用C语言每天定时做个压缩文件之类的问题
linfeng0810 2009-06-24
  • 打赏
  • 举报
回复
举的例子不太好..
每天下午5点,调用一个流程...
不可以隔1分钟自检一次,太不效率
kldx5092 2009-06-24
  • 打赏
  • 举报
回复
修改

private static System.Timers.Timer timeClock = new System.Timers.Timer();

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
timeClock.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEvent);

timeClock.Interval = timespan; // 这里设置第一次执行时离 8:00:00的时间差
timeClock.AutoReset = true;
timeClock.Enabled = true;

}

private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
{
// do something
......

timeClock.Interval = 24 * 60 * 1000.0;// 以后每隔24小时执行一次
timeClock.AutoReset = true;
timeClock.Enabled = true;

}

kldx5092 2009-06-24
  • 打赏
  • 举报
回复
在Global.asax中


private static System.Timers.Timer timeClock = new System.Timers.Timer();

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
timeClock.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEvent);

timeClock.Interval = tr.Interval * 24 * 24 * 60 * 1000.0;
timeClock.AutoReset = true;
timeClock.Enabled = true;

}

private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
{
// do something
}
zgke 2009-06-24
  • 打赏
  • 举报
回复
用计划任务把.
desegou 2009-06-24
  • 打赏
  • 举报
回复
你判断时间是不是8点,是的话就蹦,当然前提是你写的程序要在运行,不想手动运行可以考虑把程序设为开机运行

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧