1.定时器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;namespace WebApplication1
{public class Global : System.Web.HttpApplication{void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码Timer time = new Timer();time.Interval = 1000;time.Enabled = true;time.AutoReset = true;time.Elapsed += new ElapsedEventHandler(TimeEvent);}private void TimeEvent(object source, ElapsedEventArgs e){int intHour = e.SignalTime.Hour;int intMinute = e.SignalTime.Minute;int intSecond = e.SignalTime.Second;int iHour = 10;int iMinute = 30;int iSecond = 00;//每天10:30开始执行程序if (intHour == iHour && intMinute == iMinute && intSecond == iSecond){WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");} }public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}void Application_End(object sender, EventArgs e){//  在应用程序关闭时运行的代码
}void Application_Error(object sender, EventArgs e){// 在出现未处理的错误时运行的代码
}void Session_Start(object sender, EventArgs e){// 在新会话启动时运行的代码
}void Session_End(object sender, EventArgs e){// 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件。
}}
}

2.cache

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Caching;namespace WebApplication1
{public class Global : System.Web.HttpApplication{const string key="key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码
HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}void Application_End(object sender, EventArgs e){//  在应用程序关闭时运行的代码
}void Application_Error(object sender, EventArgs e){// 在出现未处理的错误时运行的代码
}void Session_Start(object sender, EventArgs e){// 在新会话启动时运行的代码
}void Session_End(object sender, EventArgs e){// 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件。
}}
}

与想要的不一致

  private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); }

也不对,说明不是writeStream的问题

  void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Add(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); }

还错

   const string key = "key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan .FromSeconds (5), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(5), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); }

 const string key = "key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan.FromMinutes (1), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){if (HttpRuntime .Cache[key] != null){HttpRuntime.Cache.Remove(key);}HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}

发现很好

从以前的可以看出最小间隔时间是20s

  const string key = "key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan.FromSeconds (20), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){if (HttpRuntime .Cache[key] != null){HttpRuntime.Cache.Remove(key);}HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(20), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}

好了

3.threading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.Threading;namespace WebApplication1
{public class Global : System.Web.HttpApplication{static AutoResetEvent wait = new AutoResetEvent(false);void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码object state = new object();ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 5000, false);}public void test(object state, bool timedOut){WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}void Application_End(object sender, EventArgs e){//  在应用程序关闭时运行的代码
}void Application_Error(object sender, EventArgs e){// 在出现未处理的错误时运行的代码
}void Session_Start(object sender, EventArgs e){// 在新会话启动时运行的代码
}void Session_End(object sender, EventArgs e){// 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件。
}}
}

 static ManualResetEvent wait = new ManualResetEvent(false);        void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码object state = new object();ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 5000, false);}public void test(object state, bool timedOut){WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}

http://www.cnblogs.com/Kazaf/archive/2012/03/26/2417341.html

转载于:https://www.cnblogs.com/hongdada/archive/2013/03/26/2983514.html

Asp.net 定时任务相关推荐

  1. 推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler...

    在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自 ...

  2. asp python 定时任务_python定时任务最强框架APScheduler详细教程

    APScheduler定时任务 上次测试女神听了我的建议,已经做好了要给项目添加定时任务的决定了.但是之前提供的四种方式中,她不知道具体选择哪一个.为了和女神更近一步,我把我入行近10年收藏的干货免费 ...

  3. asp python 定时任务_Python定时任务轻量解决方案——Schedule

    写后端的同学们可能都知道,工作中可能需要周期性执行一些任务,俗称定时任务.Linux环境下,可以借助于系统自带的crontab完成定时任务.但是很多时候,开发的同学们可能并没有权限去操作crontab ...

  4. asp python 定时任务_Ubuntu使用crontab定时执行python脚本

    转载其他博主的博客,记录过程,及使用中的一些问题 有时会遇到需要在linux的主机上定时的执行某个python脚本来处理事情的需求,这时可以考虑使用crontab来实现. 关于crontab的介绍,参 ...

  5. ASP.Net定时任务执行

    原料: System.Timers.Timer():通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序.消息没有特别的要求:缺点是不支持直接的拖放,需要手工编码. Timer的 ...

  6. C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器

    必须JobManager初始化 方式1: public void Start()         {             JobManager.AddJob(() => FetchingDa ...

  7. 基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度

    在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler>和<简单.轻量.功能非常强大的C#/ASP.NET定时调度 ...

  8. ASP.NET Core+Quartz.Net实现web定时任务

    点击蓝色"Dotnet Plus"关注我哟 加个"星标",每天清晨 07:25,干货推送! 作为一枚后端程序狗,项目实践常遇到定时任务的工作,最容易想到的的思路 ...

  9. ASP.NET Core 使用 Hangfire 定时任务

    定时任务组件,除了 Hangfire 外,还有一个 Quarz.NET,不过 Hangfire .NET Core 支持的会更好些. ASP.NET Core 使用 Hangfire 很简单,首先,N ...

最新文章

  1. python 编程入门-python编程入门(第3版)
  2. C++ Multisets
  3. airpods2使用_如何使用AirPods和AirPods Pro:完整指南
  4. matlab第六章课后答案,matlab作业第6章
  5. python基础6-函数的参数
  6. j2ee 现在已经改名为java ee_Java EE 已经正式更名为 Jakarta EE(雅加达)
  7. IntelliJ IDEA入门教程:如何使用工具窗口
  8. ArcGIS:使用镶嵌功能将地理空间数据云下载的多幅DEM合并为一幅
  9. 如何将自己的电脑做成服务器
  10. 苹果手机怎么设置信号服务器,苹果手机信号怎么改成数字 方法介绍
  11. 【优化布局】基于粒子群算法求解带出入点的车间布局优化问题附matlab代码
  12. 4、web(js):js 操作table: insertRow(),deleteRow(),insertCell(),deleteCell()方法
  13. 如何读一个在职在线的海外名校的计算机硕士
  14. Toy例程导读(三).高级语言分析和转换
  15. 【Warshall算法】
  16. C语言中带负数的除法
  17. 三维地图app的发展,离不开Infortrend GSe Pro 高效NAS共享
  18. Mybatis实现分页功能
  19. ghost系统之家Ghost XP SP3加强版V8.0_2010.4[NTFS版]
  20. WL2803E25-5/TR超低压差 低压静态电流 高PSRR CMOS LDO

热门文章

  1. 高级PHP工程师所应该具备的专业素养
  2. 强大的jQuery图片查看器插件Viewer.js
  3. POJ :3614-Sunscreen
  4. SChema中group指示器的使用
  5. ZOJ 2562 More Divisors
  6. 调用IOS邮件系统发送邮件
  7. 小学生计算机课堂实践的重要性,浅谈小学信息技术教育重要性.doc
  8. mysql 判断质数_java之判断输入的数是否为素数
  9. 三瞬属性matlab,matlab:out of memory 1
  10. 处理 JavaScript 异步操作的几种方法总结