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