Asp.net 中的重复计时器
我正在尝试实现一个重复的计时器函数asp.net。我无法创建 Windows 服务,因为我在共享环境中托管该站点,因此无权访问。
我读过各种实现这一目标的方法,每种方法都有各自的优点/缺点。 缓存对象方法看起来很有前途,但看起来确实像是一种黑客攻击。
我一直在尝试实现一个 httphandler,它将启动一个 System.Threading.Timer 对象,每分钟循环一次并执行一些工作。可能正在排队其他工作项目。
这是我到目前为止所得到的:
public class Scheduler : IHttpHandler
{
protected static TimerCallback tcb = null;
protected static Timer timer = null;
static Scheduler()
{
tcb = new TimerCallback(DoWork);
timer = new Timer(tcb, null, TimeSpan.Zero, new TimeSpan(0, 0, 1, 0));
}
private static void DoWork(Object stateInfo)
{
}
public void ProcessRequest(HttpContext context)
{
}
public bool IsReusable
{
get { return false; }
}
}
我读过 此处,您需要注意在 appDomain 卸载时定时器不会被释放。他确实暗示,只有当您调用本机代码时这才是问题,而我不是。我不知道如何结合到 application_end 事件来从处理程序中处理计时器。
我的问题是,上述方法是否偏离了目标?有更好的方法吗?放弃静态变量并将计时器存储在应用程序状态中是否更有意义?
抱歉,我需要知情的意见。我感觉我在兜圈子。
谢谢,
I am trying to implement a reoccurring timer function asp.net. I am not able to create a windows service as I host the site on a shared environment and therefore do not have access.
I have read various ways of achieving this each with their own advantages/disadvantages. The cache object approach seemed promising but does seem like a hack.
I have been trying to implement a httphandler that will spin up a single System.Threading.Timer object to cycle every minute and do some work. Possibly queuing up other work items.
Here is what I have so far:
public class Scheduler : IHttpHandler
{
protected static TimerCallback tcb = null;
protected static Timer timer = null;
static Scheduler()
{
tcb = new TimerCallback(DoWork);
timer = new Timer(tcb, null, TimeSpan.Zero, new TimeSpan(0, 0, 1, 0));
}
private static void DoWork(Object stateInfo)
{
}
public void ProcessRequest(HttpContext context)
{
}
public bool IsReusable
{
get { return false; }
}
}
I read here that you need to mindful of the timer not being disposed of when the appDomain unloading. He does imply that it is only a problem if you are invoking native code which I am not. I couldn't figure out how to tie into the application_end event to dispose of the timer from within the handler.
My question is, Is the above approach way off the mark? Is there a better way to do this? Would it make more sense to ditch the static variables and store the timer in application state?
Sorry I need informed opinions. I feel like i'm going around in circles.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个完整的黑客,希望我不会被投票! - 但是您可以开发一个组件,该组件是您的 Web 部署的一部分,该组件向同一站点上的处理程序发出 Http 请求 - 这样(我猜)您的应用程序将能够调用自身以确保它没有被卸载。查看 WebClient 类 - 抱歉,我不确定哪个名称空间。
如果您这样做,您将需要考虑如果它因任何原因被删除,如何重新启动它;我不确定如果您有一个长时间运行的网络应用程序,您是否会遇到任何奇怪的行为。
This is a complete hack, hope I don't get down voted! - but you could develop a component which is part of your web deployment which makes an Http request to a handler on the same site - this way (I guess) your app would be able to call itself to ensure it wasn't unloaded. see the WebClient class - sorry not sure which namespace off the top of my head.
If you do this you'll need to think about how it's restarted if it's taken down for any reason; and I'm not sure if you'll get any weird behaviour if you have a web application that stays up for really long periods of time.