Without Me It Just Aweso
Without Me It Just Aweso

Reputation: 4883

c# cross-thread error with UserControls

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

Upvotes: 1

Views: 671

Answers (2)

Bob
Bob

Reputation: 3351

This is probably because the handle for the control has not yet been created. If you reference dataform.Handle in your constructor, it should create the handle and set the thread ID appropriately, so InvokeRequired will return true later.

You can also force the creation of a handle with CreateControl, but only if the control is visible.

Upvotes: 4

Henk Holterman
Henk Holterman

Reputation: 273711

When you add a Control (or Form) to a parent, it sets of the Creating of WindowHandles. Apparently it is also needed to initialize the Execution context for the InvokeRequired logic.

So, don't assume that a created but never-shown Control or Form behaves 'normally'.

Upvotes: 1

Related Questions