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.

[507 byte] By [alexjstubbs] at [2008-1-10]
# 1

This means you're calling it before the control has been created, and you can't do that. You need to create the control first.

cgraus at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
oh, and the error doesnt even occur until about 60 seconds after the form in which hosts the control has been loaded. This is why i dont understand it.
alexjstubbs at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

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.

BJohnson at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
I'm sure im doign something wrong, i am new to C#, i've only been doing this for 2 months and i've only had 6 months of C++ expirience as well. Im basicly learning off examples and internet tutorials.

Heres some of the code which errors, the whole file would be way to long to post, hopefully this could help?



ServerSockets.cs:
// This does not own the form, nor any form, its simply the code for the socket/stream:
...
public static void Conc() {
Thread ConnectionThread = new Thread(new ThreadStart(Connect));
ConnectionThread.Start();
}

public static void Connect()
{

NetworkStream stream;
TcpClient ccon;
string iLine;
StreamReader reader;
try
{

ccon = new TcpClient(server, port);
stream = ccon.GetStream();
reader = new StreamReader(stream);

while (true)
{
while ((iLine = reader.ReadLine()) != null)
{

// Heres the problem:

object[] pline = { iLine };
onyx.Status ostatus = new onyx.Status();
ostatus.richTextBox2.BeginInvoke(new onyx.Status.StatDel(ostatus.ThrowMess), pline);

}

writer.Close();
reader.Close();
ccon.Close();
}
}
...



StatusScreen.cs:
...
// This is the form in which hosts the RTB to append the string iLine from above

private void Status_Load(object sender, EventArgs e)
{
onyx.Sockets.Server_Sockets.Conc();
}

public delegate void StatDel(string m);
private void ThrowMess(string m)
{
richTextBox2.AppendText(m);
}

alexjstubbs at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

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.

BJohnson at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

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...

alexjstubbs at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

I took a look back at your code and it seems that you're instantiating a new instance of the Form object and then updating the RichTextBox on it. I'm assuming that this isn't the same instance of the form that is being displayed? Is it possible that this is the problem?

BJohnson at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8
Wow, why didnt that register. Thank you very much. It was that simple =P

It works great now, thank you.

alexjstubbs at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9
  1. if (!lctx.portController1.IsOpen)
  2. {
  3. try
  4. {
  5. lctx.portController1.Open(ComPort);
  6. }
  7. catch (Exception ex)
  8. {
  9. MessageBox.Show("Unable to open the port. Please do check the port selected. Technical Info:" + ex.Message);
  10. return;
  11. }
  12. lctx.portController1.Write("AT\r");
  13. Thread.Sleep(1000);
  14. lctx.portController1.Write("ATZ\r");
  15. Thread.Sleep(1000);
  16. lctx.portController1.Write("ATH\r");
  17. Thread.Sleep(1000);
  18. lctx.portController1.Write("ATS0=3\r");
  19. }

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.

RameshKumar.S at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...