Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
想象一下,我有一个 aspx 页面,它是我网站的主页,用户在上面花费时间。现在我需要在发生事件后,将此页面变为更新。例如,如果一个新用户加入网站,所有用户都会在他们的主页上看到它。我使用 ajax 计时器来解决我的问题,并且每隔一秒调用一种方法,如果我的网站已更改,我可以将其展示给用户。但这不是一个好的解决方案。请告诉我如何在上述示例的中断后立即触发方法。(我认为当我们收到一些新电子邮件时,雅虎邮件也会使用这种方式) 感谢您的帮助。
您可以使当前线程“睡眠”直到出现新的更改。每个 Web 请求对应一个线程。并且您需要使用全局集合来跟踪所有当前正在休眠的线程。
var lastMsgTime=Global.NewMsgTime; while(true){ Thread.Sleep(300); if(!Response.IsClientConnected||RequeGlobal.NewMsgTime!=lastMsgTime){ lastMsgTime!=Global.NewMsgTime; break; } }
您仍将在客户端使用 ajax。