C#如何定时执行程序(例如每天晚上12:00)

发布时间:2009年06月04日      浏览次数:909 次
C#如何定时执行程序(例如每天晚上12:00)
--------------------------------------------------------------------------------
类似于java里面的Timer.Schedule TimeTask
谢谢
--------------------------------------------------------------------------------
c#也有timer~~
System.Timer
或者是 System.Theading.Timer
具体用法可以查询下msdn
--------------------------------------------------------------------------------
使用Timer
System.Timers.Timer aTimer = new System.Timers.Timer();
public RechargeFrm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
components=null;
AutoTime.SelectedIndex=0;
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
// Create a new Timer with Interval set to 3 seconds.
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
--------------------------------------------------------------------------------
// Only raise the event the first time Interval elapses.
aTimer.AutoReset = true;
aTimer.Enabled = flag;
//aTimer.Interval=Int32.Parse(AutoTime.SelectedItem.ToString())*300;//9s
aTimer.Interval=Int32.Parse(AutoTime.SelectedItem.ToString())*1000*60;//30m
//Console.WriteLine("Press \'q\' to quit the sample.");
//while(Console.Read()!='q');
--------------------------------------------------------------------------------
c#也有Timer,另外也可以用Thread,或者windows的计划任务
private Timer _timer;
private int _Interval=30000;
_timer = new Timer();
_timer.Enabled = true;
_timer.Interval = _Interval;
_timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
//todo something
}
--------------------------------------------------------------------------------
最简便的方法就是将定时执行交给Windows的计划任务来完成;
或者实现一个Windows服务
--------------------------------------------------------------------------------
Windows的计划任务是个不错的方法!
--------------------------------------------------------------------------------
那如何指定每天晚上12:00执行?
或者说晚上12:00第一次执行,然后每3小时执行一次
--------------------------------------------------------------------------------
我现在需要指定 第一次程序执行的具体时间
--------------------------------------------------------------------------------
上面的方法都是延迟在什么时候发生的,楼主要的是”按时“执行的,
UP一下。
--------------------------------------------------------------------------------
用计划任务不就可以啦?自己写的话要写windows服务。
--------------------------------------------------------------------------------
还是timer控制比较好吧.
--------------------------------------------------------------------------------
ohyear()理解我的意思了
计划任务根本不可行,因为我的应用是asp.net,而且是放在虚拟主机上
timer控制只能实现定时执行,不能指定具体开始执行的时间,例如晚上12:00开始执行
--------------------------------------------------------------------------------
stringCurrTime=System.DateTime.Now.ToShortTimeString();
string s="12:00";
if( CurrTime==s)
{ //程序执行代码
}
--------------------------------------------------------------------------------
xjpeng(海风) 的改一下:
private Timer _timer;
private int _Interval=30000;
_timer = new Timer();
_timer.Enabled = true;
_timer.Interval = _Interval;
_timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
//todo something
}
在todo something那里写下面的:
stringCurrTime=System.DateTime.Now.ToShortTimeString();
string s="12:00";
if( CurrTime==s)
{ //程序执行代码
}
这样应该可以了吧?
--------------------------------------------------------------------------------
UP
--------------------------------------------------------------------------------
mark
--------------------------------------------------------------------------------
最简便的方法就是将定时执行交给Windows的计划任务来完成;
或者实现一个Windows服务
----------------------------------------------------------
how to implement the windows servic?
My email is: lalac@163.com
please contact me if you know it!
Thank you
--------------------------------------------------------------------------------
学习
--------------------------------------------------------------------------------
得到时间的办法可以执行
要是此时c#程序已经关闭了呢?怎么得到时间
--------------------------------------------------------------------------------
还是只能加入计划任务吧
--------------------------------------------------------------------------------
egxsun():
这样子可行是可行,程序看上去总是有点土
看来.net的类库里面没提供类似于java的方法
--------------------------------------------------------------------------------
关注!
--------------------------------------------------------------------------------
关注。。。
同样的问题:
如果机器程序都关闭了,system.time还能取得吗?
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
asp.net做不了。IIS端不能主动做任何事的。要不你就在自己机器上写个计划任务每天定时去请求那个虚拟主机一下,让它执行事先写好的任务。
所谓定时12点一般也不过是用Timer,隔个几秒钟去问一下系统到没到12点啊?到了就执行这种。
--------------------------------------------------------------------------------
www.moblog.net.cn/sunshine.com这个软件定时任务 功能 而且是 不用编译入程序 直接用反射调用你的dll
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
MSDN
--------------------------------------------------------------------------------
直接用C#自己的Timer类就完全可以实现的?
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!