-1

I'm currently trying to update my control (Label.Text) from my BackgroundWorker thread.

Control1 mycontrol = new Control1();

mycontrol.Invoke((MethodInvoker)delegate { mycontrol.statusmainlabel.Text = "Setting up"; });

But I get the following error:

System.InvalidOperationException: "Invoke or BeginInvoke cannot be called for control until the window handle is created."

I'm trying to update my UserControl from my other class that is called by the BackgroundWorker.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Without a [mcve] that reproduces the problem, it's impossible to know for sure how _your_ code should be fixed. However, see duplicates for general advice to lead you to better comprehension of the clearly-worded error message and strategies for fixing it. As for the code you did post, if you are literally creating a `Control1` object for the sole purpose of calling `Invoke()`, that's wrong. You need to use a control instance that already exists in your form. – Peter Duniho Jul 20 '20 at 01:54

1 Answers1

-1

You need to add your control to an exist container control which has been shown.

For example:

Control1 mycontrol = new Control1();
form1.Controls.Add(mycontrol);
//then update it
逍遥子k
  • 177
  • 5
  • It is more likely that the OP should not be creating the new control in the first place. In any case, it's not possible to know what the answer is given the lack of a [mcve]. – Peter Duniho Jul 20 '20 at 02:11