Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c# - How to call function on timer ASP.NET MVC

I need to call function on timer (lets say onTickTack() function) and reload some info in ASP.NET MVC project. I know that there are several ways to do that, but which one is the best by your opinion?

Note: the function should be called from one place only, and should be called every X mins till application is up.

EDIT 1: Reload some info - for example I have something in the Cache and I want to update it from DB on timer - once a day in certain time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer to this question would very much depend on what do you mean by reload some info in ASP.NET MVC project. This is not a clearly stated problem and as such, more than obviously, it cannot have a clearly stated answer.

So if we assume that by this you want to periodically poll some controller action and update information on a view you could use the setInterval javascript function to periodically poll the server by sending an AJAX request and update the UI:

window.setInterval(function() {
    // Send an AJAX request every 5s to poll for changes and update the UI
    // example with jquery:
    $.get('/foo', function(result) {
        // TODO: use the results returned from your controller action
        // to update the UI
    });
}, 5000);

If on the other hand you mean executing some task on the server at regular periods you could use the RegisterWaitForSingleObject method like this:

var waitHandle = new AutoResetEvent(false);
ThreadPool.RegisterWaitForSingleObject(
    waitHandle, 
    // Method to execute
    (state, timeout) => 
    {
        // TODO: implement the functionality you want to be executed
        // on every 5 seconds here
        // Important Remark: This method runs on a worker thread drawn 
        // from the thread pool which is also used to service requests
        // so make sure that this method returns as fast as possible or
        // you will be jeopardizing worker threads which could be catastrophic 
        // in a web application. Make sure you don't sleep here and if you were
        // to perform some I/O intensive operation make sure you use asynchronous
        // API and IO completion ports for increased scalability
    }, 
    // optional state object to pass to the method
    null, 
    // Execute the method after 5 seconds
    TimeSpan.FromSeconds(5), 
    // Set this to false to execute it repeatedly every 5 seconds
    false
);

If you mean something else, don't hesitate to provide more details to your question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...