ASP. NET interval for some time to execute a method

Source: Internet
Author: User

Imagine that the website background automatically updates the value of cache["test" every second, and through this implementation, we can do some time to update the database at the interval of the operation.

1, define an event class Bmaevent, add the processor method in the interval 1 seconds update the cache:

  Public classbmaevent {Private StaticTimer _timer;//Timer        Staticbmaevent () {_timer=NewTimer (NewTimerCallback (Processor),NULL, +, +); }        /// <summary>        ///This method is empty and only acts as an activation Brnmall event handling mechanism/// </summary>         Public Static voidStart () {}/// <summary>        ///Event Handlers/// </summary>        /// <param name= "state" >Parameter Object</param>         Public Static voidProcessor (ObjectState ) {HttpRuntime.Cache.Insert ("Test", DateTime.Now.ToString ()); }    }

2. Start the event in Global.asax under Application_Start ()

Bmaevent.start ();

3. Display part, add in controller

 Public actionresult Btnclick ()        {            string' initialize ... " ;             if (httpruntime.cache["test"null)               str= httpruntime.cache["test"]. ToString ();             return Content (str);        }

4, add the display code in the page, using the Ajax

<div id="result"></div>@Ajax. ActionLink (" Get value " " Btnclick " New " result " })

ASP. NET interval for some time to execute a method

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.