This article uses C# to share the code of a small timer program
I always thought that the timer program was so mysterious. Later, when I actually wrote a small timer program myself, I found that In fact, it is not as difficult as imagined. Below, I will share my own operation process, hoping it will be helpful to everyone.
1) Add the reference file in our project: TaskSchedulerEngine.dll (dll defines an ITask interface, which defines two Methods Initialize and HandleConditionsMetEvent);
2) Create a regularly triggered class: SyncTask.cs (define the class name yourself), which must implement the interface ITask. The specific code is as follows:
public class SyncTask : ITask { //接受传递过来的参数的变量 private string configName; /// <summary> /// 具体操作的代码 /// </summary> public void HandleConditionsMetEvent(object sender, ConditionsMetEventArgs e) { try { // 此处为具体的操作 } catch (Exception ex) { //抛出异常,记录错误日志 } } /// <summary> /// 初始化 /// </summary> /// <param name="schedule"></param> /// <param name="parameters">参数(该参数在定时触发设置时传递)</param> public void Initialize(ScheduleDefinition schedule, object parameters) { //通过传递过来的参数来初始化变量 configFileName = parameters.ToString(); try { //初始化的具体代码 } catch (Exception e) { //抛出异常,记录错误日志 } } }
3) Configure the app.config file, parameter setting instructions for config file:
a.
b.
<taskSchedulerEngine> <schedule> <at name="TaskName" month="*" dayOfMonth="*" dayOfWeek="*" hour="*" minute="58" second="0" kind="Local"> <execute> <task type="Test.Task.SyncTask, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" parameters="FtpConfig.xml" /> </execute> </at> </schedule> </taskSchedulerEngine>
4) Main program to start the timing program:
SchedulerRuntime.StartWithConfig();
OK , so far, a complete timing program has been written. Friends, you are welcome to give your valuable opinions.
The above is the detailed content of Using C# to realize timing applet code sharing. For more information, please follow other related articles on the PHP Chinese website!