Home > WeChat Applet > Mini Program Development > Using C# to realize timing applet code sharing

Using C# to realize timing applet code sharing

高洛峰
Release: 2017-03-14 16:25:02
Original
2311 people have browsed it

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)
    {
   //抛出异常,记录错误日志 
    }
  }
}
Copy after login

3) Configure the app.config file, parameter setting instructions for config file:

a. Yes If a Task triggers different programs at different times, you need to set multiple ; name: It is the name of each , you can name it according to your own needs; month: In which month the Task is triggered , * means it triggers every month; dayofMonth: what day of the month it triggers, * means every day; dayOfWeek: what day of the week it triggers, * means it triggers every day; hour: what time it triggers every day, * means it triggers once every hour. ; minute: Triggers at a few minutes per hour, 58 means triggers at 58 minutes per hour; second: Triggers at a few seconds per minute.

b. 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 no parameters are passed, they can be set to "";

<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>
Copy after login

4) Main program to start the timing program:

SchedulerRuntime.StartWithConfig();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template