System. Threading. Timer 사용 방법

23411 단어 reading

:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml#


System.Threading.Timer timer = null; // timer=new System.Threading.Timer(Send, null, 0, 10000); //Send Send(object obj), 0 ,10000 Timer ---- Posted on 2008-08-21 10:24 (3607) (6) , 、 、 、 。 , , 。 。 , , bug , Exception , , , 。 , , , Exception 。 System.Threading.Timer , , , 。 public Timer ( TimerCallback callback, Object state, TimeSpan dueTime, TimeSpan period ) callback TimerCallback , 。 state , ( Visual Basic Nothing)。 dueTime TimeSpan, callback 。 -1 。 (0) 。 period callback 。 -1 。 、 TimerCallback Timer 。 , 。 , ThreadPool 。 , ( ) ( )。 Change 。 , Dispose 。 , WaitHandle Dispose(WaitHandle) 。 ,WaitHandle 。 , ThreadPool 。 : dueTime period , callback 。 dueTime (0), callback。 dueTime -1 , callback; , Change 。 period (0) -1 , dueTime , callback; , Change 。 using System; using System.Threading; public class TestTimer { /// <summary> /// /// </summary> private Timer iTimer; /// <summary> /// constructor /// </summary> public TestTimer() { iTimer = new System.Threading.Timer(new TimerCallback(Doing)); iTimer.Change(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); } /// <summary> /// /// </summary> /// <param name="nObject"></param> public void Doing(object nObject) { //do something } } : 。 , , , 。 , 。 、 。 、 、 。 /// <summary> /// /// </summary> public class PaperManager { /// <summary> /// /// </summary> private Timer iTimer; /// <summary> /// /// </summary> private TimeSpan dueTime; /// <summary> /// /// </summary> private TimeSpan period; /// <summary> /// /// </summary> private TimerCallback timerDelegate; /// <summary> /// /// </summary> private static readonly PaperManager self = new PaperManager(); /// <summary> /// /// </summary> public PaperManager() { timerDelegate = new TimerCallback(CheckStatus); } /// <summary> /// /// </summary> /// <returns></returns> public static PaperManager getInstance() { return self; } /// <summary> /// /// </summary> /// <param name="days"> </param> /// <param name="hours"> </param> /// <param name="minutes"> </param> /// <param name="seconds"> </param> /// <param name="milisecond"> </param> public void setDueTime(int days, int hours, int minutes, int seconds, int milisecond) { dueTime = new TimeSpan(days, hours, minutes, seconds, milisecond); } /// <summary> /// /// </summary> /// <param name="days"> </param> /// <param name="hours"> </param> /// <param name="minutes"> </param> /// <param name="seconds"> </param> /// <param name="milisecond"> </param> public void setPeriod(int days, int hours, int minutes, int seconds, int milisecond) { period = new TimeSpan(days, hours, minutes, seconds, milisecond); } /// <summary> /// /// </summary> public void Start() { AutoResetEvent autoEvent = new AutoResetEvent(false); dueTime = TimeSpan.FromSeconds(0); period = TimeSpan.FromSeconds(10); iTimer = new Timer(timerDelegate, autoEvent, dueTime, period); autoEvent.WaitOne(5000, false); iTimer.Change(dueTime, period); } /// <summary> /// /// </summary> public void Stop() { iTimer.Dispose(); } /// <summary> /// /// </summary> public void ExcuteOneTime() { if (iTimer != null) { iTimer.Dispose(); } // period (0) -1 , dueTime , callback; // , Change 。 setDueTime(0, 0, 0, 0, 1); setPeriod(0, 0, 0, 0, -1); AutoResetEvent autoEvent = new AutoResetEvent(false); iTimer = new Timer(timerDelegate, autoEvent, dueTime, period); autoEvent.WaitOne(5000, false); iTimer.Change(dueTime, period); } /// <summary> /// /// </summary> /// <param name="nObject"></param> public void CheckStatus(object nObject) { AutoResetEvent autoEvent = (AutoResetEvent)nObject; if (ExcuteUpdate()) { autoEvent.Set(); } } /// <summary> /// /// </summary> /// <returns></returns> private bool ExcuteUpdate() { try { // Paper , //List<Paper> paperList = getPaperList(); List<Paper> paperList = new List<Paper>(); foreach (Paper item in paperList) { if (item.EndTime <= DateTime.Now) { if (item.Status == Paper.StatusOfNormal) { item.Status = Paper.StatusOfTerminate; } } } //// , return true; } catch { return false; } } } , 。 /// <summary> /// /// </summary> public class Paper { /// <summary> /// /// </summary> public DateTime EndTime; /// <summary> /// /// </summary> public int Status; /// <summary> /// /// </summary> public const int StatusOfNormal = 1; /// <summary> /// /// </summary> public const int StatusOfTerminate = 2; /// <summary> /// /// </summary> /// <param name="status"></param> /// <param name="endTime"></param> public Paper(int status, DateTime endTime) { Status = status; EndTime = endTime; } }

좋은 웹페이지 즐겨찾기