Here is my situation:
I instantiated a c# usercontrol on a main thread, but it is not added to the form.
//global declaration
usercontrol1 datacontrol;
constructor()
{
.......
datacontrol = new usercontrol1();
........
}
I then have an asyhcnronous background thread that fires an event that is handled by the instantiated datacontrol. The event handler has a:
if(InvokeRequired){BeginInvoke(..);}
This should prevent any cross-threaded calls from being made. However when this gets called InvokeRequired is false so the handler is not invoked on the correct thread. So in the handler when I attemped a this.labelname.text ="blah" a cross-thread exception is thrown.
However if I add the control to a panel on the mainform, and remove it, then allow the background thread to fire the event. The handler enters but this time 'InvokeRequired' is set to true so it properly invokes itself in the mainthreads context avoiding the exception.
Can someone explain to me why the act of adding it to a panel then removing it fixes the error?
There is no onLoad events for the form so everything should be properly instantiated without it being drawn.
thanks! stephanie