Use of timed task library

1 Download the library through Nuget: taskschedulerengine

Creator: pettijohn.com

 

2 Use of timed tasks, add classes, as follows:

    public class DataSyncTask : ITask
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        public void HandleConditionsMetEvent(object sender, ConditionsMetEventArgs e)
        {
            try
            {
                // 此处为具体的操作
            }
            catch (Exception ex)
            {
                //抛出异常,记录错误日志
                logger.Fatal(ex.Message, ex);
            }
        }

        public void Initialize(ScheduleDefinition schedule, object parameters)
        {

        } 
    }

3 Configuration file node configuration method: <task type="namespace. Class name, namespace,...

  <taskSchedulerEngine>
    <schedule>
      <at name="TaskName" month="*" dayOfMonth="1" dayOfWeek="*" hour="1" minute="1" second="1" kind="Local">
        <execute>
          <task type="SheBaoGIS.DataSyncTask, SheBaoGIS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" parameters="" />
        </execute>
      </at>
    </schedule>
  </taskSchedulerEngine>
</configuration>

4) The main program to start the timing program:

SchedulerRuntime.StartWithConfig();

 Quoting other websites to add:

c# Timing running program sharing (timing program)

 Updated: January 7, 2014 09:10:16 Author:     I want to comment

 

I wrote a small timing program. After the timing is triggered, you can run other codes as required. Share the operation process. I hope it will be helpful to everyone.

1) Add a reference file in our project: TaskSchedulerEngine.dll (dll defines an ITask interface and defines two methods Initialize and HandleConditionsMetEvent);

2) Create a time-triggered class: SyncTask.cs (the class name is defined by yourself), this class must implement the interface ITask. The specific code is as follows:

 

Copy the code code as follows:


public class SyncTask: ITask
{   //The variable that accepts the passed parameters   private string configName;     /// <summary>   /// The code of the specific operation   /// </summary>   public void HandleConditionsMetEvent(object sender, ConditionsMetEventArgs e)   {     try     {       // Here is the specific operation     }     catch (Exception ex)     {       // Throw an exception and record the error log     }   }


  













 

  /// <summary>
  /// Initialization
  /// </summary>
  /// <param name="schedule"></param>
  /// <param name="parameters"> parameters (this parameter is triggered at timing Passed during setting)</param>
  public void Initialize(ScheduleDefinition schedule, object parameters)
  {             //Initialize variables through the passed parameters     configFileName = parameters.ToString();     try     {       //Specific code for initialization     }     catch (Exception e )     {           //Throw an exception and record the error log      }   } }











 

3) Configure the app.config file, the parameter setting description of the configuration file:

a. <at></at> is a task. If different programs are triggered at different times, you need to set multiple <at>; name: is the name of each <at>, you can name it according to your needs; month: The month in which the task is triggered, * means it is triggered every month; dayofMonth: the day of each month to be triggered, * means every day; dayOfWeek: the day of the week to trigger, * means it is triggered every day; hour: what time of day Trigger, * means trigger once every hour; minute: trigger several minutes per hour, 58 means trigger 58 minutes per hour; second: trigger several seconds per minute.

b. <task> is the class that needs to be triggered, type: "the detailed address of the class that needs to be triggered (project name. folder name. class name), project name, Version, Culture, PublicKeyToKen", parameters: parameters that need to be passed , If you don’t pass parameters, you can set it to "";

 

Copy the code code as follows:


    <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) The main program to start the timing program:

 

Copy the code code as follows:


SchedulerRuntime.StartWithConfig ();

 

OK, so far, a complete timing program is finished.

Guess you like

Origin blog.csdn.net/qq503690160/article/details/86308489