Hi,
If you are using multithreading this issue can happen. This is because, if you are trying to access a control which is created in an other thread form a different thread, it will throw an exception. In the normal case, controls will be created in the designer thread, that is the STA thread. Your lengthy function will be in a user defined thread. When it is accessing the control from designer, it is a cross thread access and it will leads to the exception. To avoid that what you can do is, whenever you want to access the contrl from the STA thread, use the code as below.
Here i am explaining like i have a label control and i want to update the text property from a different thread.
i have a function Update status.
public void UpdateStatus(int percentage, string message)
{
if (InvokeRequired)
{
BeginInvoke(new UpdateStatusDelegate(UpdateStatus), new object[] { percentage, message });
return;
}
this.lblProgress.Text = message;
}
In this function whenever there is a call it will check whether it is from the current there(if not Invoke required will be true). If it is from a different thread it will use a delegate to execure the same in the thread where the UI control is there. Hope this will help you.
-- Thanks Ajith R [Mark as Answer if it is Helpful.]