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