asp.net timed execution procedure

Source: Internet
Author: User
Tags current time diff time interval

Using timer to solve the problem is very simple, first set the Timer class interval attribute (in milliseconds), that is, the time interval, and then in the timer elapsed event to write the execution code, every time a set of interval interval, The events in the elapsed will be executed (this is basically the same as the timer control in the VB program).
That knows what class to use, where is this code written? Write the code in the Global.asax, right-click on the item on VS, click Add--New item, select Global Application class, there will be Global.asax files in the project.

To express clearly the code directly (first introduce the system.timers named control in the Global.asax file header):

<span style= "FONT-SIZE:18PX;" > Public    class Global:System.Web.HttpApplication
    {
        //This code starts protected void at the same time as the site is running
        application_ Start (object sender, EventArgs e)
        {
            
            System.Timers.Timer objtimer = new System.Timers.Timer ();
            Objtimer.interval = 60*1000;    This time unit: milliseconds
            objtimer.enabled = true;        Sets the availability of the Timer class
            //Binds the timer's Elapsed event to the newly created timer object
            objtimer.elapsed + = new Elapsedeventhandler (objtimer_ Elapsed); 

        } </span>

The following is the code in the timer's elapsed event

<span style= "FONT-SIZE:18PX;" >void objtimer_elapsed (object sender, Elapsedeventargs e)
{

            
            string time = DateTime.Now.ToShortTimeString () //Get current time
			//time to get current setting from configuration file.
            string ordertime = configurationmanager.appsettings["Orderfoodtime"];

            /* Test Data */If
            (Time.equals (ordertime))
            {
                //If the time is equal, perform the action you want to perform, here you can call the methods of the other classes in your program
            }
}</span >

This will achieve the purpose of the scheduled execution of the program.

This is used in my project on a regular basis, in the forum asked about the timing of the implementation of the program, but also said that the use of Windows services more secure

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.