利用Cache,让asp.net 简单实现定时执行任务

 

代码
private static CacheItemRemovedCallback OnCacheRemove = null;

protected void Application_Start(object sender, EventArgs e)
{
    AddTask(
"DoStuff"60);
}

private void AddTask(string name, int seconds)
{
    OnCacheRemove 
= new CacheItemRemovedCallback(CacheItemRemoved);
    HttpRuntime.Cache.Insert(name, seconds, 
null,
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    
// do stuff here if it matches our taskname, like WebRequest
    
// re-add our task so it recurs
    AddTask(k, Convert.ToInt32(v));
}


相关文章:

  • 2021-05-21
  • 2021-11-13
  • 2021-08-08
  • 2022-12-23
  • 2021-12-15
  • 2021-10-27
猜你喜欢
  • 2021-09-27
  • 2021-09-20
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-10-26
相关资源
相似解决方案