日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Asp.net 定时任务

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。