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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Reading and Writing CSV Files in C#A much simpler way to have your application share data is by reading and writing Comma-Separated Values (CSV) files. CSV...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.