I have a windows.form.userControl class and at run time i want to add some linklabels dynamically. When I apply this code snippet inside Load method it work perfectly.
for (int i = 0; i < 10; i++)
{
linkLabel = new System.Windows.Forms.LinkLabel();
linkLabel.Name = i.ToString();
linkLabel.Text = i.ToString();
linkLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(linkLabel_LinkClicked);
this.Controls.Add(linkLabel);
linkLabel.Top = top;
top += 30;
}
But when I move this code snippet inside to backgroudworker doWork method it will give invalid operation exception related to cross thread issue in this line :- this.Controls.Add(linkLabel);
How do I make this as a thread safe operation? I'm newbie to C# and I'm using C# 4.0 using VS 2010. Thanks in advance.