2 threads, 1 GUI, 1 tcp client... 1 bug

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Okay:
I have a GUI program that initializes a TcpClient and a Form. From withing
the form I create a TextBox and init the client. Then I run a
System.Threading.Timer with a method that checks if the TcpClient has
available data, and if it does, it tries to send that data as text to the
TextBox. But, of course I get an Exception - Illegal cross-thread operation -
because I am not allowed to access the Control (TextBox) from a thread it
wasn't created in.
How do I do this? I understand I'm supposed to create a delegate etc., but
I've never done such a thing and I'n confused. If someone could explain it
better, preferably with a code example, I'll be very thankful.
Just for emphasis - I want to change a Form TextBox from a thread it wasn't
created on.

Thanks!
 
yoav said:
Okay:
I have a GUI program that initializes a TcpClient and a Form. From withing
the form I create a TextBox and init the client. Then I run a
System.Threading.Timer with a method that checks if the TcpClient has
available data, and if it does, it tries to send that data as text to the
TextBox. But, of course I get an Exception - Illegal cross-thread operation -
because I am not allowed to access the Control (TextBox) from a thread it
wasn't created in.
How do I do this? I understand I'm supposed to create a delegate etc., but
I've never done such a thing and I'n confused. If someone could explain it
better, preferably with a code example, I'll be very thankful.
Just for emphasis - I want to change a Form TextBox from a thread it wasn't
created on.

See http://www.pobox.com/~skeet/csharp/winforms.shtml
 
Back
Top