c# - How to call function on timer ASP.NET MVC -
i need call function on timer (lets onticktack() function) , reload info in asp.net mvc project. know there several ways that, 1 best opinion?
note: function should called 1 place only, , should called every x mins till application up.
edit 1: reload info - example have in cache , want update db on timer - once day in time.
the answer question depend on mean reload info in asp.net mvc project. not stated problem , such, more obviously, cannot have stated answer.
so if assume want periodically poll controller action , update information on view use setinterval javascript function periodically poll server sending ajax request , update ui:
window.setinterval(function() { // send ajax request every 5s poll changes , update ui // example jquery: $.get('/foo', function(result) { // todo: use results returned controller action // update ui }); }, 5000);
if on other hand mean executing task on server @ regular periods use registerwaitforsingleobject method this:
var waithandle = new autoresetevent(false); threadpool.registerwaitforsingleobject( waithandle, // method execute (state, timeout) => { // todo: implement functionality want executed // on every 5 seconds here // important remark: method runs on worker thread drawn // thread pool used service requests // make sure method returns fast possible or // jeopardizing worker threads catastrophic // in web application. make sure don't sleep here , if // perform i/o intensive operation make sure use asynchronous // api , io completion ports increased scalability }, // optional state object pass method null, // execute method after 5 seconds timespan.fromseconds(5), // set false execute repeatedly every 5 seconds false );
if mean else, don't hesitate provide more details question.
Comments
Post a Comment