• ASP.NET中设置一个定时器来定时更新 转


     asp.net 定时器 比较少用,  中国红木网
    这是一个相当实用的功能,有了RSS博客镜像,就不需要在多处同时发布博客日志了。比如你同时在新浪上有自己的博客,又同时有自己的个人博客站点,那么你只需要在新浪上发布博客日志,而个人博客站点通过RSS博客镜像功能将新浪博客上发布的新日志自动更新到站点中。
    我们在ASP.NET的,至于asp,由于其天生的局限性,可能无法直接在Web应用程序中完成这一功能,而需要额外的Windows应用程序的支持。
    1. 管理RSS博客镜像
    首先我们需要在RSS博客镜像,需要管理的内容有:
    l RSS地址
    l 更新频率
    l 最后更新的时间

    需要管理的内容如图所示,这里所列出的3个是必须的字段,当然根据你自己系统的需要,你可能需要更多的字段。
    这部分工作由于和RSS博客镜像,和你本身的系统也是比较相关的,这里就不再多做介绍,你根据自己系统的需要去实现就行。
    2. 通过ASP.NET的定时器来抓取RSS源
    有了上面的对每个RSS种子ASP.NET中设置一个定时器来定时更新这些RSS种子内容了。
    这部分内容请主要参考如下文章《在 ASP.NET 中使用计时器(Timer)》 博客中的实现方式:,根据这篇文章实现定时器就足够了。下面我主要介绍下在
    这下面的代码都在Global.ascx.cs中实现。
    protected void Application_Start(Object sender, EventArgs e)
    {

    SetApplicationStatus(RssMirror, true);

    // 设置定时器
    System.Timers.Timer timer = new System.Timers.Timer();

    timer.Elapsed += new System.Timers.ElapsedEventHandler(this.RefressRssMirror);

    timer.Interval = 300000; // 每5分钟中触发定时器
    timer.AutoReset = true;

    timer.Enabled = true;

    }

    private void SetApplicationStatus(string keyword, object result)
    {
    Application.Lock();
    Application[keyword] = result;
    Application.UnLock();
    }

    private void UpdateRssMirror(fmRssMirror item, fmblog.Data.DataProviders.DataProvider provider)
    {
    try
    {
    DateTime now = DateTime.Now;

    XmlDocument doc = new XmlDocument();

    // 载入RSS种子
    doc.Load(item.FeedUrl);

    // 解析RSS种子内容
    fmPostCollection list = fmFeedParser.GetPosts(item.UserName, doc, item.LastUpdateTime);

    for(int i=list.Count-1; i>=0; i--)
    {
    // 根据标题,判断博客日志是否已经存在
    if(provider.GetPost(item.UserName, list[i].Title)==null)
    {
    provider.NewPost(list[i], string.Empty);
    }
    }

    // 设置最后更新时间
    provider.SetRssMirrorLastUpdateTime(item.Id, now, item.UserName);
    }
    catch(Exception)
    {
    }
    }

    protected void RefressRssMirror(object sender, System.Timers.ElapsedEventArgs e)
    {
    // 判断上次触发的定时器是否已经完成
    if((bool)Application[RssMirror])
    {
    SetApplicationStatus(RssMirror, false);

    try
    {
    fmblog.Data.DataProviders.DataProvider provider = fmblog.Data.DataProviders.DataProvider.CreateInstance(Application);

    provider.OpenConnection();

    try
    {
    fmRssMirrorCollection list = provider.GetAllRssMirrors();

    DateTime now = DateTime.Now;
    // 更新RSS博客镜像里的所有RSS种子
    foreach(fmRssMirror item in list)
    {
    TimeSpan span = now - item.LastUpdateTime;

    if(span.Hours>=item.UpdateInterval)
    {
    UpdateRssMirror(item, provider);
    }
    }
    }
    catch(Exception)
    {
    }

    provider.CloseConnection();
    }
    catch(Exception)
    {
    }

    SetApplicationStatus(RssMirror, true);
    }
    }

  • 相关阅读:
    Oracle调优总结--(经典实践 重要)
    ORACLE索引介绍和使用
    ORACLE索引介绍和使用
    oracle update 改为 merge
    oracle update 改为 merge
    在 Eclipse 下利用 gradle 构建系统
    在 Eclipse 下利用 gradle 构建系统
    关于SQL查询效率,100w数据,查询只要1秒
    关于SQL查询效率,100w数据,查询只要1秒
    Add Binary
  • 原文地址:https://www.cnblogs.com/lljinz/p/3779421.html
Copyright © 2020-2023  润新知