Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Cross thread exception
 

Cross thread exception

I have a form whith a split container, in the right side of the split container a i want to add dinamic a list of controls (I have a list o questions that i get from the database) . I have more than 100 controls to add to the right side of the split container, and i want to display a progress bar control. I have try to call ansyncron the creation of the controls but when i want to add the control list to the right side of the split container throws me an cross thread exception.


Does anyone has any sugestion?
Cross thread exception  Wednesday, April 29, 2009 2:39 PM
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.]
Ajith R Nair  Thursday, May 07, 2009 8:03 AM
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.]
Ajith R Nair  Thursday, May 07, 2009 8:03 AM

You can use google to search for other answers

Custom Search

More Threads

• Access folder
• Form Controls toolbar
• how to play the flash videos in the Windows Forms?
• Default Control Modifier
• Problem while Deleting the first band from Infragistics control
• Strange behaviour when overriding TextBox.Text
• Menus in Inherited forms
• Alternate colors for cells in a table in RichTextBox?
• how to validate masktextbox
• Nested Forms, better button graphic, layout???