Hi,
I am creating a code where thread is used on start button.In the Mythread method ,I am accessing the class which read and write bytes using serial port. And in main form progress bar is used to show the reading and writing process is start or end.And this progressBar Method is called by using event and delegate. My problem is when I start debugging the code, as soon as it comes to event which handle progress bar Method the Cross Thread exception error is occured. If I comment the code in method it will debug next code smoothly. So is it possible to debug the entire code without commenting my progressbar method so I can Check the code in that perticular method also.
Please Help Me!!

Posted Aug 28, 2008, 2:02 PM
You cannot modify a form control from a thread unless the thread owns that control. Otherwise you will get a cross-thread exception because this action is not thread safe. If you want to modify the control, you must use a delegate to allow the control to be modified on its owning thread.
Example:
Label labelOne = new Label(); // This label is created on a thread named ThreadOne
string myString = "Hello!"; // This string is created on a thread named ThreadTwo, a different thread.
// Doing this from ThreadOne is fine.
labelOne.Text = myString;
// However, doing the same from ThreadTwo is ILLEGAL without the use of a delegate.
// Create the delegate
delegate void UpdateLabelText(string updateText);
// When you want to update the text on the label, do as such:
if (labelOne.InvokeRequired)
{
// If an invoke is required, invoke the delegate using the control.
labelOne.Invoke(new UpdateLabelText(UpdateLabel), myString);
}
else
// If no invoke is required, you can just call the method outright.
UpdateLabel(myString);
// You must specify a method for the delegate to execute on the context of the controls owning thread. This method MUST also match the signature of the delegate.
void UpdateLabel(string newLabelText)
{
labelOne.Text = newLabelText;
}
Hope this helps!