Reputation: 2748
The line of code below when called from a non UI thread causes the error:
System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
this.formWaitingForm
.BeginInvoke((MethodInvoker)(() =>
this.formWaitingForm.ShowDialog(this)));
Upvotes: 0
Views: 1977
Reputation: 17185
Yea, this doesn't work. You cannot invoke on a control that hasn't been created. Forms must be created on the main thread, too. Did you maybe intend to invoke onthis
instead?
this.BeginInvoke(()=>{
Form f=new MywaitForm();
f.Showdialog(this);
});
This should do the trick, if I understand the problem correctly.
Explanation: BeginInvoke
or Invoke
post a special message to the message loop of the given window (the one on the left side of the call, that is). This message will be processed by the thread that created that window and is handling user interaction. Once the thread sees such a special message, the code will execute in its context. Therefore, the target object of the Invoke needs to be an active window.
Upvotes: 0
Reputation: 9571
It means the Control
s handle hasn't been created yet. See the IsHandleCreated property.
This can happen for a variety of reasons. You can force the handle to get created by calling the CreateControl method.
This still may not work:
The CreateControl method forces a handle to be created for the control and its child controls. This method is used when you need a handle immediately for manipulation of the control or its children; simply calling a control's constructor does not create the Handle.
CreateControl does not create a control handle if the control's Visible property is false. You can either call the CreateHandle method or access the Handle property to create the control's handle regardless of the control's visibility, but in this case, no window handles are created for the control's children.
So you may need to try grabbing the Control.Handle
value to force the handle to get created, depending on your situation.
Upvotes: 1