When some tasks that need to be performed timed, such as: Timed backup database, timing acquisition network data, etc., you need to plan tasks. Window also has a planned task but there are many shortcomings that must be manually configured, and it is also very troublesome to deploy, and the accuracy of time is one minute, and the application configuration tasks can only be configured. The task program implemented here is that it can be programmed, which can be flexible, and the accuracy of time is one second, and is not achieved by polling time. The efficiency is relatively high. You can write a service in the Windows service, use it to manage all planned task logic.
The library source code is as follows: / ************************************************** *********************************************************** (C) 2003-2005 C2217 Studio * All rights reserved * * File Name: Task.cs * File ID: * Programming Language: C # * File Description: Implement the scheduled task's scheduling mechanism. You can implement a variety of task scheduling mechanisms (define time accurate to second): * 1. Immediately (once) * 2. Program execution (once, define task begins to execute the date) * 3. Immediately (loop execution, multiple times ) * 4. Program implementation (loop executes repeatedly, define the date to start the task execution) * Interface can be achieved by developing their own implementation plans ISchedule * * current version: 1.0 * alternate versions: * * author: Deng Yang are * Email: DYJ057@gmail.com 0 Create Date: 2005-4-8 * Last modified Date: 2005-4-8 * * History Modification Record:
*********************************************************** ************************************************* / Using System; use system.collections Using system.threading; // using nunit.framework;
Namespace IBMS.UTILITY.TASK {
#REGION Task Plan Interface and Some Standard Implement ///
///
}
///
Public DateTime ExecutionTime {Get {// Todo: Add immediatelyshedule.executionTime getter Implement Return DateTime.now;} set {;}}
Public long duetime {get {return 0;}}
PUBLIC Long Period {Get {// Todo: Add ImmediatelyShedule.Period getter Implement Return Timeout.Infinite;}} #ENDREGONTE
///
///
Private datetime m_schedule;
#Region ischedule member
Public DateTime ExecutionTime {get {// Todo: Add ScheduleExecutionOcut.executionTime GetTer Implement Return M_SCHEDULE;} set {m_schedule = value;}}
///
IF (ms <0) ms = 0; Return MS;}}
PUBLIC Long Period {Get {// Todo: Add ScheduleExecutiononce.Period getter Implement Return Timeout.Infinite;}}
#ndregion}
///
///
Private datetime m_schedule; private timeespan m_period; #Region ischedule member
Public long duetime {get {long ms = (m_schedule.ticks- datetime.now.ticks) / 10000;
IF (ms <0) ms = 0; Return MS;}}
Public DateTime ExecutionTime {Get {// Todo: Add Cycexecution.executionTime GetTer Implement Return M_SCHEDULE;} set {m_schedule = value;}
} PUBLIC Long Period {Get {// Todo: Add Cycexecution.Period GetTer Realization Return M_Period.Ticks / 10000;}}
#ndregion
}
#ndregion
#REGION Task Implementation ///
/// (2) Write his own task to the TimerCallback commission, then generate an instance of a Task class, // / / set its Job and JobParam properties, and then Start can start the service. It is no longer possible to use the task management center. ///, for example: /// Task newtask = new task (new immediateexecution ()); /// newtask.job = new timercallback (newtask.execute); /// newtask.jobparam = "test immedialte task"; // Add your own parameters /// newtask.start (); /// /// summary> public class task {///
m_schedule = schedule;}
///
///
if (m_schedule.Period == Timeout.Infinite) {m_nextExecuteTime = DateTime.MaxValue; // next run time does not exist} else {TimeSpan period = new TimeSpan (m_schedule.Period * 1000); m_nextExecuteTime = m_lastExecuteTime period;} }
///
DateTime m_nextexecutetime; ///
PRIVATE ISCHEDULE M_SCHEDULE;
///
Private timerCallback m_exectask;
///
///
}
#ndregion
#REGON boot task ///
///
///
///
///
Task.start ();
///
///
// Test Immedialte Task Task NEWTASK = New Task (New ImmediateExecution ()); NewTask.Job = New TimerCallback (NewTask.execute); NewTask.jobParam = "Test Immedialte Task";
// Test Excute Once Task DateTime Sheduletime = DateTime.now.addSeconds (10); ScheduleExecutiononce Future = New ScheduleExecutiononCE (Sheduletime);
Task sheduletask = new task; sheduletask.job = new timerCallback (Sheduletask.execute); Sheduletask.jobParam = "Test Excute Once Task"; // Test Cyc Task At Once
Cycexecution CYC = New Cycexecution (New Timespan (0,0,2)); task cystask = new task; cystask.job = new timerCallback (cystask.execute); cystask.jobparam = "Test Cyc Task";
// Test Cys Task At Schedule
Cycexecution cycshedule = new cycExecution (DateTime.now.addseconds (8), New Timespan (0,0,2));
Task cycsheduletask = new task; cycsheduletask.job = new timercallback (cysticuletask.jobparam = "test cyc shedule task);
Center.addtask; center.addtask; center.addtask; center.addtask;
Center.startAlltask ();
Console.readline ();
Console.writeline (NewTask.lasteexecuteTime);}}
#ndregion}
Test code: using system.threading;
Namespace ibms.utility.task.test {///
Public override void execute (object param) {// must keep Base.execute (param);
Console.writeline ("Begin to Execute A Long Job ... nextExecutetime: {0}", this.nextexecutetime);
Thread.Sleep (5000); console.writeline ("Test Tast Execute Job.last ExecuteTime {0},", this.lastexecutetime);
}
///
// Test Immedialte Task Task NEWTASK = New Testtask (New ImmediateExecution ()); NewTask.jobParam = "Test Immedialte Task";
// Test excute once task DateTime sheduleTime = DateTime.Now.AddSeconds (10); ScheduleExecutionOnce future = new ScheduleExecutionOnce (sheduleTime); Task sheduleTask = new TestTask (future); sheduleTask.JobParam = "Test excute once task"; // Test Cyc Task At Cycexecution CYC = New Cycexecution (New Timespan (0,0,2)); Task Cystask = New Testtask (CYC); cystask.jobparam = "Test Cyc Task";
// Test cys task at schedule CycExecution cycShedule = new CycExecution (DateTime.Now.AddSeconds (8), new TimeSpan (0,0,2)); Task cycSheduleTask = new TestTask (cycShedule); cycSheduleTask.JobParam = "Test cyc Shedule Task; center.addtask; center.addtask; center.addtask; center.addtask;
Center.startAlltask ();
Console.readline (); console.writeline ("Terminate All Task");
Center.TerminateAllTask (); console.readline ();
}
}