Implementing a background timing task for a Web site

Source: Internet
Author: User

1, why the Web site to use background scheduled task

Perhaps someone to ask, the Web site itself on the background service, why also want to customize the background timing task.

is really simple, as anyone who has done a real web application knows. A real site often has to do some background processing, such as statistics, evaluation, data updates, and other operations, some of these tasks are regularly repeated, of course, the administrator can regularly perform our development of the Web page click event (You know, IIS is a client response to perform the daemon).

Another case is that some click to perform the operation, the IIS response time is relatively long, such as the author of an application last year, is to evaluate a region of XX problem, because the background to perform a model system, click after 3 minutes to wait until the results (haha, Is this waiting for you to suffer? )。 This time, you can use the background timing task to achieve early, the results are stored or broken down into a fast response task.


2, how to implement

The author first thought of the timer, in their own development, in accordance with the previous habit of surfing the internet first check the results of predecessors, did not think, really someone thought, also done. This is no longer analyzed, the following is an excerpt from the original text:

Background processing technology of asp.net

In asp.net web development, there is often a problem: the slow response of user actions.

The reason for this may be that the user operation is a time-consuming operation, generally. NET program design when the user submits the operation is in the background processing, wait until the processing is completed and then return the operation results to the user. In a smaller system this design is simple, and the performance will not have much problem, but in a larger system this design will give users a bad operation experience, affect the user's impression of the system good or bad.

In my previous implementation of the system there are generally two ways to deal with this situation: first, the user's operations directly logged to the background database, by the background program to perform a regular scan. Second, the use of asp.net timing processing, directly in the Web server layer to deal with.

The difference between the two ways is not very large, the first way the main needs of a background program to complete the scan.

I'm here to briefly introduce the second way.

Its core processing is System.Threading.Timer. This timer class can be used to perform user commit operations in the background,


How it is used: System.Threading.TimerCallback t = new System.Threading.TimerCallback (your processing method);
System.Threading.Timer t = new System.Threading.Timer (t, NULL, 1000, 5000);

This section shows that the specified agent is invoked every 5 seconds after starting 1 seconds.

I defined three classes when I implemented them.

1, Bkexecitem is used to save user submissions, and it can also be serialized to disk to avoid the loss of critical background tasks.

2, bkexec for execution. It invokes the method specified in Bkexecitem by reflection. In addition, it maintains a first entry in the middle

First Out queue queue<bkexecitem>, this queue records all the background processing items.

3, the Bkmanager completes the timer initialization, the module parameter configuration and so on function.

Well, to sum up here for a moment. Next time I will post the code also for your reference. A practical asp.net background processing class

Well, this time I'll talk to you about the asp.net background processing, and will put our current project in the application of a background processing class code for reference.

Background processing is also a problem that needs to be considered in the management system design now.

What is background processing, you can simply think that it is not in user process processing to complete the user committed operations, but rather put this processing into the server-side background process to deal with.

After adding background processing, it can improve the operation speed of front desk users and improve the user operation experience.

For the average user, his basic requirement for a system is to respond in a timely manner, it is difficult for users to wait 10 seconds after a commit operation of the management system to generate goodwill, but in the actual system running user operation is very difficult in a short time to get the response, so this time background processing can play a role.

In the following code, I'll define the tasks that require background processing as a Execitem object. After the user submits the operation, the system will turn the operation into a Execitem object to join the first in-first out queue in the Bkexecmanager (Background processing management object).

The Web site starts Bkexecmanager automatically when it starts, and Bkexecmanager starts a timer to handle the background task queues at timed intervals.

Bkexecmanager removes the Task object from the queue when processing completes, and notifies the administrator by mail to complete the problem if the operation fails.

Oh, now put the code!

1, Background processing management Objects

public class Bkexecmanager
... {
Timed callback.
private static TimerCallback timerdelegate;
private static Timer Statetimer;
private static Bkexecer M_execer;
public static string DataPath;
public static string Bkmanager = "XXXX";
public static int bkbufsize = 100;
private static int Interval = 10000;

public static Bkexecer Execer
... {
Get ... {return m_execer;}
}

Static Bkexecmanager ()
... {
DataPath = System.AppDomain.CurrentDomain.BaseDirectory + "bkitem/";
if (system.configuration.configurationmanager.appsettings["Interval"]!= null)
Interval = Convert.ToInt32 (system.configuration.configurationmanager.appsettings["Interval"));

if (system.configuration.configurationmanager.appsettings["bkbufsize"]!= null)
Bkbufsize = Convert.ToInt32 (system.configuration.configurationmanager.appsettings["BkBufSize"));

if (system.configuration.configurationmanager.appsettings["Bkmanager"]!= null)
Bkmanager = system.configuration.configurationmanager.appsettings["Bkmanager"];

M_execer = new Bkexecer ();
Initializing callbacks
Timerdelegate = new TimerCallback (m_execer.dobkexec);
Initialize Timer
Statetimer = new Timer (timerdelegate, NULL, 5000, Interval);
}

/**////<summary>
Stop the timer.
</summary>
static void Bkexecquit ()
... {
Statetimer.dispose ();
}
}

2, background processing execution

public class Bkexecer
... {
Maintains a queue that goes forward.
Private queue<execitem> m_bkexecitemlist;
private static Object Lockhelper = new Object ();
private static bool M_isbusy = FALSE;

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.