views:

48

answers:

1

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

+3  A: 

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.

Bob
The option for .createHandle() doesnt show up for my usercontrol. .IsHAndleCreated does though
Without Me It Just Aweso
@Without Sorry, CreateHandle is protected. You can access it inside of your control class. Updated answer with another alternate solution. Just accessing the Handle property should do what you want, though.
Bob
this worked!! thank you so much. I was getting so confused by this!!
Without Me It Just Aweso
Just to share what I did.I added a (!datacontrol.IsHandleCreated) datacontrol.CreateControl()after the initial declaration of the control. This seems to have fixed it. thanks again!
Without Me It Just Aweso