ASP. Net scheduled task execution, ASP. Net task execution
Raw materials:
System. timers. timer (): Pass. NET Thread Pool is lightweight, accurate timing, and has no special requirements on applications and messages. The disadvantage is that it does not support direct drag and drop and requires manual encoding.
Main attributes of Timer:
1. Interval: Interval at which Elapsed events are triggered;
2. Elapsed: events that occur at intervals;
3. Enabled: Indicates whether Timer should trigger the Elapsed event;
Design:
I. TaskBase: Task Base Class
1. Field:
A. TimeMode. The task types include cycle and time;
B. LastStartTime
2. attributes:
A. NextStartTime, read-only attribute
If (this. TimeMode = 0)
{
If (this. LastStartTime = DateTime. MinValue)
{
Return DateTime. MinValue;
}
Return this. LastStartTime. AddSeconds (double) this. MinPeriod );
}
If (this. LastStartTime = DateTime. MinValue)
{
Return this. TheFixedNextStartTime;
}
Return this. LastStartTime. AddDays (1.0). Date. AddHours (double) this. Hour). AddMinutes (double) this. Minute );
B. TheFixedNextStartTime; read-only attribute
If (this. _ theFixedNextStartTime = DateTime. MinValue)
{
This. _ theFixedNextStartTime = DateTime. Today. AddHours (double) this. Hour). AddMinutes (double) this. Minute );
If (SystemStartTime. Hour * 100) + SystemStartTime. Minute)> (this. Hour * 100) + this. Minute ))
{
This. _ theFixedNextStartTime = this. _ theFixedNextStartTime. AddDays (1.0 );
}
}
Return this. _ theFixedNextStartTime;
3. Method:
1. Execute: Specific execution of a task
2. Run: Run the task.
This. LastStartTime = DateTime. Now;
This. Execute ();
Ii. TaskMonitor: Job monitoring
1. Method
A. Execute: Timer. Elapsed event
If (TimeMode = 1)
{
Flag = DateTime. Now> = base2.NextStartTime;
}
Else
{
Flag = (base2.TickCount = 0) & (base2.LastStartTime = DateTime. MinValue ));
If (! Flag)
{// Comparison of the timer running interval and task running Interval
Base2.TickCount + = CheckPeriod;
Flag = base2.TickCount> = base2.MinPeriod;
}
If (flag)
{
Base2.TickCount = 0;
}
}
If (flag)
Thread. RunMethod (new ThreadStart (base2.Run ));