程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 在asp.net頁面中使用異步同步rss

在asp.net頁面中使用異步同步rss

編輯:關於ASP.NET

有的時候我們需要在網頁裡讀取論壇的信息,在傳統ASP的時候我們使用的是JS或者是IFRAME,這兩種方式都不是很方便,而且對搜索引擎不友好。現在有了.Net,我們有了另一種方式。

要求:論壇需要提供RSS支持。

代碼如下:

#region task class
   //這是一個任務類,執行具體的任務
   public class RssAsyncTask
   {
     private String _rssContent;
     private AsyncTaskDelegate _dlgt;
     private string rssUrl;
     private bool _success;

     public bool IsSuccess
     {
       get
       {
         return _success;
       }
     }

     public RssAsyncTask(string rssUrl)
     {
       this.rssUrl = rssUrl;
     }

     // Create delegate.
     protected delegate void AsyncTaskDelegate();

     public String GetRssContent()
     {
       return _rssContent;
     }
     public void DoTheAsyncTask()
     {
       // Introduce an artificial delay to simulate a delayed
       // asynchronous task. Make this greater than the
       // AsyncTimeout property.
       WebClient wc = new WebClient();
       try
       {
         _rssContent = wc.DownloadString(rssUrl);
         _success = true;
       }
       catch (Exception e)
       {
         _rssContent = e.Message;
       }
       finally
       {
         wc.Dispose();
       }
       //Thread.Sleep(TimeSpan.FromSeconds(5.0));
     }

     // Define the method that will get called to
     // start the asynchronous task.
     public IAsyncResult OnBegin(object sender, EventArgs e,
       AsyncCallback cb, object extraData)
     {
       //_rssContent = "Beginning async task.";

       _dlgt = new AsyncTaskDelegate(DoTheAsyncTask);
       IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);

       return result;
     }

     // Define the method that will get called when
     // the asynchronous task is ended.
     public void OnEnd(IAsyncResult ar)
     {
       //_rssContent = "Asynchronous task completed.";
       _dlgt.EndInvoke(ar);
     }

     // Define the method that will get called if the task
     // is not completed within the asynchronous timeout interval.
     public void OnTimeout(IAsyncResult ar)
     {
       _rssContent = "Ansynchronous task failed to complete " +
         "because it exceeded the AsyncTimeout parameter.";
     }
   }
   #endregion

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved