日期:2014-05-18 浏览次数:20513 次
System.Timers.Timer timer; protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); timer = new System.Timers.Timer(1000*60*5);//5分钟 timer.Elapsed += new System.Timers.ElapsedEventHandler(Auto_Run); timer.Start(); } void Auto_Run(object sender, System.Timers.ElapsedEventArgs e) { PerformanceController controller = new PerformanceController(); controller.AutoRun(); }
------解决方案--------------------
这个写个windows服务比较好:
public partial class MyServer : ServiceBase { public MyServer() { InitializeComponent(); this.ServiceName = "MyServer"; } Thread th = null; bool isStop = false; protected override void OnStart(string[] args) { if (th == null) { th = new Thread(new ThreadStart(delegate { while (!isStop) { //上午10点和下午4点 if ((DateTime.Now.Hour == 10 || DateTime.Now.Hour == 16) && DateTime.Now.Minute == 0) { //执行你的任务 } Thread.Sleep(60000); } })); } th.Start(); } protected override void OnStop() { isStop = true; Thread.Sleep(3000); if (th != null) { th.Abort(); th = null; } } protected override void OnPause() { } protected override void OnContinue() { } }
------解决方案--------------------
Timer定时器,右下角托盘,再放入系统启动项。我当年有个程序就是这么做的,实际情况很不错。