So I am writing a small Twitter client for me to use. I am using a combination of one big panel, with smaller panels representing the individual tweets. In each smaller panel, I have a PictureBox and a RichTextBox.
Now, my problem is that loading more than 10 tweets causes a slowdown because I am dynamically generating the panels. So I decided to do this using a BackgroundWorker and then add those panels to the main panel.
I've done this numerous times with writing text to a textbox from a different thead(even wrote tutorials on it). Yet I cannot get this to work. I get the error message:
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
Code:
List<Panel> panelList = new List<Panel>();
foreach (UserStatus friendStatus in list)
{
PictureBox pbTweet = new PictureBox();
// ...
// code to set numerous properties
// ...
RichTextBox rtbTweet = new RichTextBox();
// ...
// code to set numerous properties
// ...
Panel panelTweet = new Panel();
// ...
// code to set numerous properties
// ...
panelTweet.Controls.Add(pbTweet);
panelTweet.Controls.Add(rtbTweet);
panelList.Add(panelTweet);
}
if (panelMain.InvokeRequired)
panelMain.BeginInvoke((MethodInvoker)delegate { foreach (Panel p in panelList) { panelMain.Controls.Add(p); } });
Anybody notice any problems?