Asp.net 定时任务
生活随笔
收集整理的這篇文章主要介紹了
Asp.net 定时任务
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.定時(shí)器
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){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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開始執(zhí)行程序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){// 在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼 }void Application_Error(object sender, EventArgs e){// 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼 }void Session_Start(object sender, EventArgs e){// 在新會話啟動(dòng)時(shí)運(yùn)行的代碼 }void Session_End(object sender, EventArgs e){// 在會話結(jié)束時(shí)運(yùn)行的代碼。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為// InProc 時(shí),才會引發(fā) Session_End 事件。如果會話模式設(shè)置為 StateServer // 或 SQLServer,則不會引發(fā)該事件。 }} }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){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼 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){// 在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼 }void Application_Error(object sender, EventArgs e){// 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼 }void Session_Start(object sender, EventArgs e){// 在新會話啟動(dòng)時(shí)運(yùn)行的代碼 }void Session_End(object sender, EventArgs e){// 在會話結(jié)束時(shí)運(yùn)行的代碼。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為// InProc 時(shí),才會引發(fā) Session_End 事件。如果會話模式設(shè)置為 StateServer // 或 SQLServer,則不會引發(fā)該事件。 }} }與想要的不一致
改
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){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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"); }還錯(cuò)
改
const string key = "key";void Application_Start(object sender, EventArgs e){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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");}發(fā)現(xiàn)很好
從以前的可以看出最小間隔時(shí)間是20s
const string key = "key";void Application_Start(object sender, EventArgs e){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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){// 在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼 }void Application_Error(object sender, EventArgs e){// 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼 }void Session_Start(object sender, EventArgs e){// 在新會話啟動(dòng)時(shí)運(yùn)行的代碼 }void Session_End(object sender, EventArgs e){// 在會話結(jié)束時(shí)運(yùn)行的代碼。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為// InProc 時(shí),才會引發(fā) Session_End 事件。如果會話模式設(shè)置為 StateServer // 或 SQLServer,則不會引發(fā)該事件。 }} } static ManualResetEvent wait = new ManualResetEvent(false); void Application_Start(object sender, EventArgs e){// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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
?
轉(zhuǎn)載于:https://www.cnblogs.com/hongdada/archive/2013/03/26/2983514.html
總結(jié)
以上是生活随笔為你收集整理的Asp.net 定时任务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银行按揭借款合同范本
- 下一篇: 易语言大漠透明图制作与使用