Practical tips for using timer to implement scheduled tasks in ASP.net

Source: Internet
Author: User
Tags time interval
There are 3 types of timer available for us in the. NET Framework, respectively:
Server timer (System.Timers.Timer), Thread timer (System.Threading.Timer), and Windows Timer (System.Windows.Forms.Timer).
Windows timer, like the timer in WINAPI, is message-based and single-threaded. The other two timer is different from Windows timer, they are based on ThreadPool, so the biggest advantage is that the time interval is accurate and uniform. The difference between server timer and thread timer is that the server timer is event-based and the thread timer is based on callback.
The thread timer is lighter in comparison, so the following is an example of thread timer, which explains how to use thread timer to implement a scheduled task in asp.net.
Here's a class that implements a scheduled task with a timer:
public class Scheduledtask
{
private static readonly Scheduledtask _scheduledtask = null;
Private Timer Updatetimer = null;
Time interval, set here to 15 minutes
private int Interval = 15 * 60000;
private int _isrunning;
Static Scheduledtask ()
{
_scheduledtask = new Scheduledtask ();
}
public static Scheduledtask Instance ()
{
return _scheduledtask;
}
public void Start ()
{
if (Updatetimer = null)
{
Updatetimer = new Timer (new TimerCallback (updatetimercallback), NULL, Interval, Interval);
}
}
private void Updatetimercallback (object sender)
{
if (Interlocked.exchange (ref _isrunning, 1) = = 0)
{
Try
{
Write the tasks you want to perform here.
}
catch (Exception ex)
{
}
Finally
{
Interlocked.exchange (ref _isrunning, 0);
}
}
}
public void Stop ()
{
if (Updatetimer!= null)
{
Updatetimer.dispose ();
Updatetimer = null;
}
}
}
First, pay attention to this paragraph: private int _isrunning;
_isrunning is a flag that represents whether the task that was triggered at the previous interval is completed.
Why do we need this _isrunning sign?
Because, if we perform a long task, it can cause the task that was triggered in the previous time period to not be completed, and the next task starts again, which causes the problem of reentrant. To solve this problem, we use _isrunning as a flag to indicate whether the last task was completed, and if it was done, we would perform a new task, and if not, skip the task and proceed with the last task.
The specific logic is implemented in the following code:
Program code
private void Updatetimercallback (object sender)
{
if (Interlocked.exchange (ref _isrunning, 1) = = 0)
{
Try
{
Write the tasks you want to perform here.
}
catch (Exception ex)
{
}
Finally
{
Interlocked.exchange (ref _isrunning, 0);
}
}
}
As you can see, the code above uses the Interlocked.exchange method. The function of this method is to guarantee the security of the object assignment under multithreading. Because in multithreading, we directly to the _isrunning assignment is not safe, so in this case interlocked.exchange is useful.
Having said the implementation of the Scheduledtask class, let's look at how to call this class in asp.net.
It is recommended that you call this class in Application_Start, with the following code:
Program code
public class Global:System.Web.HttpApplication
{
protected void Application_Start (object sender, EventArgs e)
{
Scheduledtask.instance (). Start ();
}
protected void Application_End (object sender, EventArgs e)
{
Scheduledtask.instance (). Stop ();
}
}
OK, the above is a timer in the ASP.net simple application, if there is anything wrong with the place, please advise.
Related Article

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.