【发布时间】:2011-12-07 09:01:26
【问题描述】:
我正在使用 MVC 2,我有一个视图,它只显示带有当前时间的标签。
我想每 5 秒更新一次这个视图(标签),这样时间就会更新。我在下面使用(取自here),但似乎没有工作。
public ActionResult Time()
{
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
ViewData["Time"] = "Current time is: " + DateTime.Now.ToLongTimeString();
},
// 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
);
return View();
}
提前感谢您的帮助!
【问题讨论】:
-
你是从客户端调用这个吗?
标签: c# asp.net-mvc asp.net-mvc-2