2010-12-01 57 views
10

我需要在計時器上調用函數(可以說onTickTack()函數)並重新加載ASP.NET MVC項目中的一些信息。我知道有幾種方法可以做到這一點,但根據您的意見哪一個方法最好?如何在計時器上調用函數ASP.NET MVC

注意:函數應該只從一個地方調用,並且應該每X分鐘調用一次,直到應用程序啓動。

編輯1: 重新加載一些信息 - 例如我有一些在緩存中,我想從定時器上的數據庫更新它 - 在特定時間每天一次。

+2

我們需要什麼你正在嘗試做的 – rboarman 2010-12-01 22:49:03

+0

你能列舉下來的幾種方法,使我們可以給我們的意見更多的細節? – 2010-12-02 09:00:48

回答

24

回答這個問題將在很大程度上取決於你的意思就重裝在ASP.NET一些信息MVC項目。這不是一個明確說明的問題,因此,更明顯的是,它不能有明確說明的答案。

因此,如果我們假設此要定期輪詢一些控制器操作和更新視圖的信息,你可以使用setInterval JavaScript函數來發送一個AJAX請求定期輪詢服務器,並更新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); 

如果在另一方面,你的意思是在固定期限在服務器上執行一些任務中,你可以使用RegisterWaitForSingleObject方法是這樣的:

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 
); 

如果你的意思是別的東西,不HESI爲你的問題提供更多細節。

1

我通過從Application_Start()開始一個工作線程來做到這一點。

這裏是我的類:

public interface IWorker 
{ 
    void DoWork(object anObject); 
} 

public enum WorkerState 
{ 
    Starting = 0, 
    Started, 
    Stopping, 
    Stopped, 
    Faulted 
} 

public class Worker : IWorker 
{ 
    public WorkerState State { get; set; } 

    public virtual void DoWork(object anObject) 
    { 
     while (!_shouldStop) 
     { 
      // Do some work 
      Thread.Sleep(5000); 
     } 

     // thread is stopping 
     // Do some final work 
    } 

    public void RequestStop() 
    { 
     State = WorkerState.Stopping; 
     _shouldStop = true; 
    } 
    // Volatile is used as hint to the compiler that this data 
    // member will be accessed by multiple threads. 
    protected volatile bool _shouldStop; 
} 

它的開始是這樣的:

 var backgroundThread = new Thread(Worker.DoWork) 
            { 
             IsBackground = true, 
             Name = "MyThread" 
            }; 
     backgroundThread.Start(); 
11

您可以使用Timer類Glbal.asax的OnApplicationStart事件...

public static System.Timers.Timer timer = new System.Timers.Timer(60000); // This will raise the event every one minute. 
. 
. 
. 

timer.Enabled = true; 
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 
. 
. 
. 

static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
{ 
    // Do Your Stuff 
} 
0

我的解決辦法是這樣;

<script type="text/javascript"> 
    setInterval(
     function() 
     { 
      $.post("@Url.Action("Method", "Home")", {}, function (data) { 
       alert(data); 
      }); 
     }, 5000) 
</script> 

調用方法;

[HttpPost] 
public ActionResult Method() 
{ 
    return Json("Tick"); 
} 
相關問題