Using asynchronous reads in the ASP.net page

Source: Internet
Author: User
Tags define bool datetime
Asp.net| Page | asynchronous

Sometimes we need to read the forum information in the Web page, in the traditional ASP when we are using JS or IFRAME, these two ways are not very convenient, and the search engine is not friendly. Now that we have. Net, we have another way.

Requirements: The forum needs to provide RSS support.

The code is as follows:


#region Task Class
This is a task class that performs specific tasks
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 a 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'll 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" called when
        //The asynchronous task is ended.
        public void OnEnd (IAsyncResult ar)
         {
           //_rsscontent = ' Asynchronous task completed. ";
            _dlgt. EndInvoke (AR);
       }

       //Define The "method" that would 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

A custom control that inherits from another custom control.
public class Rarticle
: Lpanel
{
#region Properties
String Rssurl;

public string Rssurl
{
get {return rssurl;}
set {Rssurl = value;}
}

int maxrecordnumber = 6;

        public int Maxrecordnumber
         {
            get {maxrecordnumber;}
            set {maxrecordnumber = value;}
       }
        #endregion

Rssasynctask task;
protected override void OnInit (EventArgs e)
{
Base. OnInit (e);
task = new Rssasynctask (This.rssurl);
PageAsyncTask asynctask = new PageAsyncTask (task. Onbegin, Task. OnEnd, Task. OnTimeOut, NULL);

Page.registerasynctask (Asynctask);
Page.executeregisteredasynctasks ();
}

        static Random r = new Random ();
        protected override void Render (System.Web.UI.HtmlTextWriter writer)
        {
             string rsscontent = task. Getrsscontent ();
            XmlDocument doc = null;
            if (task. issuccess)
            {
                 doc = new XmlDocument ();
                Doc. Loadxml (rsscontent);

                this. Title = doc. selectSingleNode ("Rss/channel/title"). InnerText;
                this. Titlenavigateurl = doc. selectSingleNode ("Rss/channel/link"). InnerText;
                this. Showtitle = true;
           }
            Base. Renderbegin (writer);

Writer. WriteBeginTag ("div");
Writer. WriteAttribute ("Class", "child2");
Right (writer);
Writer. WriteBeginTag ("ul");
Right (writer);

if (Doc!= null)
{
#region Success

XmlNodeList items = doc. SelectNodes ("Rss/channel/item");
list<xmlnode> nodes = new list<xmlnode> ();
foreach (XmlNode node in items)
Nodes. ADD (node);

Use a paradigm to reverse order of dates
Nodes. Sort (New comparison<xmlnode> delegate (XmlNode N1, XmlNode n2)
{
DateTime D1 = DateTime.Parse (n1. selectSingleNode ("pubdate"). InnerText);
DateTime D2 = DateTime.Parse (n2. selectSingleNode ("pubdate"). InnerText);
TimeSpan ts = d2-d1;
return (int) ts. TotalSeconds;
}));

for (int i = 0; i < Maxrecordnumber; i++)
{
XmlNode node = nodes[i];
Writer. WriteBeginTag ("Li");
Right (writer);
Writer. WriteBeginTag ("a");
Writer. WriteAttribute ("Target", "_blank");
Writer. WriteAttribute ("href", node.) selectSingleNode ("link"). InnerText);
Right (writer);
Writer. Write (node. selectSingleNode ("title"). InnerText);
Writer. Writeendtag ("a");
Writer. Writeendtag ("Li");
}

                #endregion
           }
             Else
            {
                writer. WriteBeginTag ("Pre");
                Right (writer);
                Writer. Write (Task. Getrsscontent ());
                Writer. Writeendtag ("Pre");
           }

Writer. Writeendtag ("ul");
Writer. Writeendtag ("div");

RenderChildren (writer);

Base. Renderend (writer);
}
}

How to use:

First, register the control

<%@ Register assembly= "Controls" namespace= "Limited.controls" tagprefix= "LM"%>

Second, call

<lm:rarticle id= "RArticle1" runat= "maxrecordnumber=" rssurl= "http://bbs.5inet.net/rss.aspx"/>

For the sake of simplicity, this program does not use the technology such as caching, if necessary, please add yourself.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.