ASP.NET 定時(shí)器回調(diào)方法的重入
話不多說,請(qǐng)看代碼:
using System; using System.Collections.Generic; using System.Text; namespace NET.MST.Sixth.Reenter { class Reenter { //用來造成線程同步問題的靜態(tài)成員 private static int TestInt1=0; private static int TestInt2 = 0; private static object locko = new object(); static void Main(string[] args) { Console.WriteLine("System.Timers.Timer 回調(diào)方法重入測(cè)試:"); TimersTimerReenter(); //這里確保已經(jīng)開始的回調(diào)方法有機(jī)會(huì)結(jié)束 System.Threading.Thread.Sleep(2 * 1000); Console.WriteLine("System.Threading.Timer 回調(diào)方法重入測(cè)試:"); ThreadingTimerReenter(); Console.Read(); } /// <summary> /// 展示System.Timers.Timer的回調(diào)方法重入 /// </summary> static void TimersTimerReenter() { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 100; //100毫秒 timer.Elapsed += TimersTimerHandler; timer.Start(); System.Threading.Thread.Sleep(2 * 1000); //運(yùn)行2秒 timer.Stop(); } /// <summary> /// 展示System.Threading.Timer的回調(diào)方法重入 /// </summary> static void ThreadingTimerReenter() { //100毫秒 using (System.Threading.Timer timer = new System.Threading.Timer (new System.Threading.TimerCallback(ThreadingTimerHandler), null, 0, 100)) { System.Threading.Thread.Sleep(2 * 1000); //運(yùn)行2秒 } } /// <summary> /// System.Timers.Timer的回調(diào)方法 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void TimersTimerHandler(object sender,EventArgs args) { lock (locko) { Console.WriteLine("測(cè)試整數(shù):" + TestInt1.ToString()); //睡眠10秒,保證方法重入 System.Threading.Thread.Sleep(300); TestInt1++; Console.WriteLine("自增1后測(cè)試整數(shù):" + TestInt1.ToString()); } } /// <summary> /// System.Threading.Timer的回調(diào)方法 /// </summary> /// <param name="state"></param> private static void ThreadingTimerHandler(object state) { lock (locko) { Console.WriteLine("測(cè)試整數(shù):" + TestInt2.ToString()); //睡眠10秒,保證方法重入 System.Threading.Thread.Sleep(300); TestInt2++; Console.WriteLine("自增1后測(cè)試整數(shù):" + TestInt2.ToString()); } } } }
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
ASP.NET設(shè)計(jì)FTP文件上傳的解決方案
這篇文章主要介紹了ASP.NET設(shè)計(jì)FTP文件上傳的解決方案,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09ASP.NET Cookie 操作實(shí)現(xiàn)
本節(jié)中的主題描述如何在 ASP.NET Web 應(yīng)用程序中創(chuàng)建 Cookie。Cookie 是一些小的文本文件,服務(wù)器和瀏覽器在收到每個(gè)頁請(qǐng)求時(shí)交換它們,您還可以使用這些小文本文件來存儲(chǔ)幫助針對(duì)每個(gè)用戶自定義您的應(yīng)用程序的信息。2009-11-11ASP.NET使用SignalR2實(shí)現(xiàn)服務(wù)器廣播
這篇文章介紹了ASP.NET使用SignalR2實(shí)現(xiàn)服務(wù)器廣播的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05asp.net 中靜態(tài)方法和動(dòng)態(tài)方法調(diào)用的區(qū)別實(shí)例分析
動(dòng)態(tài)方法,在使用時(shí)需要先創(chuàng)建實(shí)例,才能調(diào)用實(shí)例方法,而靜態(tài)方法則不需要,直接使用即可。2013-06-06.NET?Core項(xiàng)目使用swagger開發(fā)組件
這篇文章介紹了.NET?Core項(xiàng)目使用swagger開發(fā)組件的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法
這篇文章主要介紹了asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法,分別以實(shí)例的形式講述了采用cookie法與session法實(shí)現(xiàn)WEB頁面防止惡意刷新的技巧,需要的朋友可以參考下2014-09-09asp.net 2個(gè)日期之間的整月數(shù)的算法
我是說兩個(gè)日期之間間隔整月,比如2008-11-5 和 2009-4-3之間的整月,結(jié)果是12,1,2,3這四個(gè)月2009-06-06ASP.NET中Application和Cache的區(qū)別分析
在asp.net中儲(chǔ)存數(shù)據(jù)的方式有很多,包括application,session,cache, cookie, viewstate。其中application和cache的應(yīng)用范圍,使用方式都比較相似,這里主要對(duì)比一下這兩種方式。2010-03-03