-
December 4th, 2011, 03:00 AM
#1
[RESOLVED] TcpClient.Connect() is preventing my textbox from updating
The texbox does not display "Connecting..." until after the connection has timed out/connected.
Code:
public void Connect(string hostname) {
textBox3.Text += "Connecting..." + n;
cSoc = new TcpClient();
try
{
cSoc.Connect(hostname, 8888);
}
catch (System.Exception)
{
success = false;
button5.Enabled = false;
textBox3.Text += "Connection timed out at address \"" + hostname + "\"";
}
if (success == true)
{
textBox3.Text += "Connected to server at(" + hostname + ")" + n;
}
try
{
serverStream = cSoc.GetStream();
}
catch (System.Exception)
{
}
}
-
December 4th, 2011, 07:07 AM
#2
Re: TcpClient.Connect() is preventing my textbox from updating
Indeed; welcome to the concept of threads. You can read a vast quantity more on the net about threads than I will present here but for a quick run-down:
When you write a program in C# and start debugging it, it enters into Main() and at some point Application.Run() is called.. So your one thread of execution starts running what is called a messaging loop; a queue of events (mouse clicks, moves, repaints etc) is constantly having new events pushed into the back as the thread is consuming them from the front. When you write a button click handler, run the program and click the button the thread comes in and does all the code you've written.. If you send it off to do some massively long operation, it disappears off to do it even though it's normally supposed to be consuming huge numbers of windowing events.. So the thread is in your code, you say textbox.text = "something" which essentially changes the text and then an instruction for the box to repaint the new text is placed in the message queue, but the thread then goes off and does the TCP connect without any processing of the message queue so of course the textbox isnt updated until the thread can get back to the queue of window messages
Windows notices your app isnt processing windowing messages any more and declares it as "not responding" .. when the long operation completes, control will return and the app will "come alive" again.. kinda like a child watching tv, so absorbed in the tv programme that he doesnt hear what you say until the programme finishes
You can solve this by calling Application.DoEvents() which causes your thread to go back and process the window queue before returning to your function, but this is bad because youre still going to do a long op and your app will look like it's hung from that point until the connect times out.. Better that you learn about backgroundworker (google it) and do all your long operations on a background thread. Never tie the windowing thread up for very long or users will complain that your app hangs and is not good to use
-
December 6th, 2011, 07:42 PM
#3
Re: TcpClient.Connect() is preventing my textbox from updating
Originally Posted by cjard
... Better that you learn about backgroundworker (google it) and do all your long operations on a background thread. ...
All correct information and good advice, but if you would prefer not to use threads, look into the asynchronous methods of TcpClient, such as BeginConnect.
-
December 8th, 2011, 03:10 AM
#4
Re: TcpClient.Connect() is preventing my textbox from updating
Or, do this:
Code:
textBox3.Text += "Connecting..." + n;
//one of these should be enough.
textBox3.Invalidate();
textBox3.Refresh();
It's not a bug, it's a feature!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|