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 that would get 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