Click here to Skip to main content
Click here to Skip to main content

Windows RSS Reader Application Using Threads

, 7 Apr 2007
Rate this:
Please Sign up or sign in to vote.
In this article, you will learn how to create an asynchronous RSS reader Windows application (using threads).

Screenshot - AsynchronousRSSReader.jpg

Introduction

In this article, you will learn how to create an Asynchronous RSS reader Windows application. The application will connect to a web server and retrieve required information from that web server. Reading RSS information from a web server is such an easy job! To do that, you just need to find a proper web server that gives you the information you're looking for and then get those information just by following a link! For example, take a look at this link.

By clicking the link, you will see the RSS of CodeProject. This RSS gives you the latest CodeProject articles. So, you can use this information in your website or your Windows application. The content of an RSS is nothing but XML and you just need to get that XML and manipulate it.

Implementation

To retrieve RSS data, you just need a DataSet! First, you have to declare it:

DataSet DTS = new DataSet();

And to read RSS data, you need to do something like this:

DTS.ReadXml("http://www.codeproject.com/webservices/articlerss.aspx?cat=1");

Just that! And, you have a DataSet that is filled with appropriate data to manipulate! But there is a really big problem! While your application is retrieving data from the server, your application will not respond until the transfer of the data is completed. To solve the problem, you need to do all of these in an asynchronous way. You can use threads, and call the method which retrieves data in another thread. Here is the method which gets data in an asynchronous way:

private void BeginGetData(string RssLink, ListView lstView, string WebServer)
{
    ArrayList arl = new ArrayList();
    arl.Add(RssLink);
    arl.Add(lstView);
    arl.Add(WebServer);

    ParameterizedThreadStart TSS = new ParameterizedThreadStart(GetRSSData);
    Thread Thread1 = new Thread(TSS);
    Thread1.Start(arl);
}

As you can see, it gets a link to the RSS server and a list view to represent the data in it and the name of the RSS server. To send those data to a thread, you need to put them all in an object data type. So, an instance of ArrayList has been created to hold the values. After that, you just need to declare the thread and send the object to it. As you can see in the code above, the method that is used by the thread is called GetRSSData.

private void GetRSSData(Object Parameters)
{
    ArrayList ar = (ArrayList)Parameters;
    string rsslink = ar[0].ToString();
    ListView lst = (ListView)ar[1];
    string WebServer = ar[2].ToString();

    DataSet DTS = new DataSet();
    try
        {
            DTS.ReadXml(rsslink);
        }
        catch
        {
            MessageBox.Show("Error getting data from server", 
                            "Error", MessageBoxButtons.OK, 
                            MessageBoxIcon.Error);
            Application.Exit();
            return;

        }
    ShowInfo(DTS, lst, WebServer);
}

In the GetRSSData, first the data that has been sent to the method is unpacked, and then the RSS link has been retrieved by this line of code:

DTS.ReadXml(rsslink);

After that, you just need to show the data, but here is another problem. You can't do that in the normal way. Because, the ListView you're trying to put the data in is not created in this thread. Only the thread that has created the ListView can manipulate it. So, you need to use another method:

delegate void ShowInfoCallback(DataSet dataset, ListView lstView, string WebServer);
private void ShowInfo(DataSet dataset, ListView lstView, string WebServer)
{
    if (lstView.InvokeRequired)
    {
        ShowInfoCallback d = new ShowInfoCallback(ShowInfo);
        this.Invoke(d, new object[] { dataset, lstView, WebServer });
    }
    else
    {
        //Processing the data
    }

    }
}

This method looks for the owner thread of the ListView. If the thread that is executing this method is the owner thread of the ListView, the data processing will occur:

DataTable tbl = new DataTable();
if (WebServer == "codeproject")
    tbl = dataset.Tables[3];
else if (WebServer == "microsoft")
    tbl = dataset.Tables[2];
else return;

lstView.Items.Clear();

for (int i = 0; i < tbl.Rows.Count; i++)
{
    ListViewItem item = new ListViewItem(tbl.Rows[i]["title"].ToString());


    if (WebServer == "codeproject")
    {
        item.SubItems.Add(tbl.Rows[i]["author"].ToString());
        item.SubItems.Add(tbl.Rows[i]["subject"].ToString());
        item.SubItems.Add(tbl.Rows[i]["pubDate"].ToString());
    }
    else if (WebServer == "microsoft")
    {
        item.SubItems.Add(tbl.Rows[i]["description"].ToString());
        item.SubItems.Add(tbl.Rows[i]["pubDate"].ToString());
    }
    item.Tag = tbl.Rows[i]["link"];
    lstView.Items.Add(item);
}

So, to read any kind of RSS data, you just need to call the BeginGetData method by providing the appropriate RSS link to it, but you may need to change some parts of the code in the ShowInfo method.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohammad Akbarizadegan
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
ASP.net developer

Comments and Discussions

 
Generaltashakor Pinmembernarmafzarboy25-May-07 1:35 
GeneralSuggestion PinmemberAlireza . Shirazi9-Apr-07 3:40 
GeneralGood Work PinmemberSyed M Hussain8-Apr-07 11:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 7 Apr 2007
Article Copyright 2007 by Mohammad Akbarizadegan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid