Error: Invoke or BeginInvoke cannot be called on a control until the window handle has been crea
I get this error when calling begininvoke from a streamed tcp connection trying to update ui in another form:
"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
Has anyone had a similar problem and fixed it? It didnt give me this problem when i called the code from the beginning of the 'try' keyword, yet it didnt update correctly there due to when it was called.
Basicly, it has to be called where i have it set... but i get this error.
I think some of code that is causing might be useful here. Specifically, I have questions about whether you're handling the TCP stream asynchronously, which might be the source of your problems. It could be that you're in a second thread (one that doesn't have a Windows form) when you're creating a control in order to call Invoke/BeginInvoke. There are still too many possibilities to come up with a definitive answer.
It would appear that threading is likely to be the culprit. If you take a look at this blog post
http://blogs.msdn.com/jfoscoding/archive/2005/05/26/422405.aspx
you'll see a description of how to use the InvokeRequired property to determine whether you need to use BeginInvoke or can just call the delegated method directly.
Oh sorry, i do check to see if it needs to be invoked... i posted older code, my fault.
I reconfigured it and now it doesnt give me this error, and it doesnt ever go through the invoke so im guessing it doesnt need to be?, it goes straight to the call on the Form thread.
It goes to a function that simply is: richTextBox2.appendtext(message);
but it does not update. So i put a messagebox.show("blah"); under that code above, and it appears so i know its calling it, and checking to see if it needs to invoke the control, which it doesnt, but it still will not append text.
I've copied examples of other peoples methods, verbatim, and still no luck. So i'm guessing it is the TCP thread which is causing this problem, so is there any advice as of things to try? Is the thread in my other post not formed correctly? So far i either get the error of the title of this thread, or i get a constantly blank RTB...
Wow, why didnt that register. Thank you very much. It was that simple =P
It works great now, thank you.
- if (!lctx.portController1.IsOpen)
- {
- try
- {
- lctx.portController1.Open(ComPort);
- }
- catch (Exception ex)
- {
- MessageBox.Show("Unable to open the port. Please do check the port selected. Technical Info:" + ex.Message);
- return;
- }
-
- lctx.portController1.Write("AT\r");
- Thread.Sleep(1000);
- lctx.portController1.Write("ATZ\r");
- Thread.Sleep(1000);
- lctx.portController1.Write("ATH\r");
- Thread.Sleep(1000);
- lctx.portController1.Write("ATS0=3\r");
- }
When the line number 14 is reached,It throws the Exception "This exception was caught by PortController but is NOT a PortController error. The following exception was generated in the event handler of the calling application:
Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
I don't know why? Any Help Please...
Thanks in Advance.