Easy Background Tasks in ASP.NET

http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/ 

 

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

 

 

posted @ 2013-03-21 20:11  Joel.Chen  阅读(210)  评论(0编辑  收藏  举报