I’ve made myself a class that reads RSS feeds and returns them to my main class. The code that I use for this is:
public List<Post> getLatestPosts()
{
this.rssReader = new XmlTextReader(this.rssUrl);
this.rssDoc = new XmlDocument();
// Load the XML content into rssDoc
rssDoc.Load(rssReader);
// ... other code to parse XML ... //
}
Now, when I call getLatestPosts() my application locks up for a few seconds. I’m assuming it’s because that’s how long it takes for the application to request the RSS feed (network latency and so forth).
I want to change this so my program doesn’t lock up, and instead just waits for the response. I had the idea of using threads in my main form, but I’m confused about how I can capture whatever RSS data getLatestPosts() gets.
If I do this in my button click on my main form:
private void bGetLatestPosts_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(rssReader.getLatestPosts()));
}
I’m not capturing anything that getLatestPosts() returns.
I’m completely new to threads (this is mostly just me messing around to try and learn them) but I do have some experience in C#.
Thanks.
I’m assuming you are writing a WinForms application. There are two important rules for writing responsive WinForms applications:
To solve your problem, run your code in a
BackgroundWorker.Here’s some code that might help you: