Esbasic reusable. Net class library (08) -- scheduled task manager timingtaskmanager

Source: Internet
Author: User

1.Origin:

Assume that our report system needs00:05:00To count the report data of the previous day00:30:00To collect the report data of last week, you need1Day00:30:00Collect the report data of the previous month.

these report statistics tasks are common system requirements, I used esbasic. threading. timers. timingtaskmanager (scheduled task manager.

TimingtaskmanagerWith the previously mentioned callback TimerCallbacktimerThe difference is that,CallbacktimerIs to refer to the current time and then wait for a period of time before execution, andTimingtaskmanagerA managed task must be executed at a specific time point.

The image of the scheduled task manager is as follows:

2.Applicable scenarios:

If your task meets the following conditions, you can useTimingtaskmanagerTo solve the scheduled task execution:

(1)The task needs to be executed at a fixed time point every hour, every day, every week, or every month.

(2)There is a certain error between the time point that allows the task to be executed and the expected time point.

 

3Design ideas and implementation

In Introduction Timingtaskmanager Before that, we should first introduce Timingtask This class represents a scheduled task, which encapsulates the execution frequency, execution time, and target method of the task. Timingtask The class diagram is as follows:

As we can see, Excutetime Attribute is Processing time Type, specifies the specific time when the task is to be executed. While Timingtasktype Attribute determines Timingtask Execution frequency, Timingtasktype Definition:

[ Enumdescription ( " Type of scheduled task " )]
Public   Enum   Timingtasktype
{
[ Enumdescription ( " Once every hour " )]
Perhour,
[ Enumdescription ( " Once a day " )]
Perday,
[ Enumdescription ( " Once a week " )]
Perweek,
[ Enumdescription ( " Once a month " )]
Permonth
}

note that, if timingtasktype the attribute value is perhour , excutetime hour attribute.

Similarly,DayofweekAttribute onlyTimingtasktypeThe property value isPerweekIt is only valid in weeks.DayAttribute onlyTimingtasktypeThe property value isPermonthValid only, indicating the number of executions per month.

In Timingtask Implementation, Isontime Special attention should be paid to the implementation of methods. Because our timer task manager is based on the timer Timer While the scanning time of the timer is separated Excutetime The two scan times at the right and right of the real execution time point may be considered to be in compliance with the execution conditions (for example, the distance between the two scan times and the real execution time is 1 In seconds). If so, the target task will be executed twice-this is not expected to happen. To avoid this situation, we Timingtask Used in Lastrighttime To record the last execution time. If Lastrighttime Difference from current time 2 If the scanning interval is less than double, the current time is deemed to be invalid. As shown below:CodeAs shown in:

# Region Prevent two executions at critical points
Timespan Span = Now -   This . Lastrighttime;
If (Span. totalmilliseconds < Checkspanseconds *   1000   *   2 )
{
Return   False ;
}
# Endregion

Next, we will focus onTimingtaskmanager. WithTimingtaskEncapsulation,TimingtaskmanagerThe things to be done are very simple. The main points are as follows:

(1) timingtaskmanager usage timer , to determine whether each task has reached the time point for execution. timerspaninsecs attribute specifies the scan interval.

(2) when a task is executed, timingtaskmanager the current foreac H traversal.

(3)TimingtaskmanagerProvidedRegistertaskAndUnregistertaskTo dynamically add or remove tasks during running.

(4) Timingtaskmanager The task list is required. Tasklist To ensure the thread security of the set. Because the timer itself is executed on another thread Worker Method, if Worker Other threads call the method at the same time. Registertask And Unregistertask Method will cause Worker Method Foreach An exception is thrown during traversal.

 

4.Precautions for use

(1) Because Timingtaskmanager Use Timer Therefore, the maximum error between the task execution time point and the expected time point is Timerspaninsecs . Because Timerspaninsecs The minimum value that can be obtained is 1 Seconds, so Timingtaskmanager The minimum error is 1 Seconds. If your task is expected to be executed more accurately Timingtaskmanager Not suitable for you.

(2) timingtasktype the specified frequency can only be: once an hour, once a day, once a week, once a month. However, if you want to 12:00:00 the work und can be used to execute tasks, it is regarded as two tasks: one at noon every Tuesday 12:00:00 run, 12:00:00 Run. In this way, we can use timingtaskmanager the most basic scheduled frequency is combined to process more advanced and complex scheduled tasks.

(3)BecauseItimingtaskexcuterOfExcuteontimeThe method is executed on a thread in the background thread pool, so any exceptions thrown by the method will be ignored. The best way isExcuteontimeThe method is to ensure thatCatchAll exceptions exist.

 

5.Extension

Scheduled Task ManagerTimingtaskmanagerThere are currently no extensions.

Note: The esbasic source code can be foundHttp://esbasic.codeplex.com/Download.
Esbasic open source Preface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.