CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2011
    Posts
    10

    [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)
                {
                    
                }
            }

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: TcpClient.Connect() is preventing my textbox from updating

    Quote Originally Posted by cjard View Post
    ... 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.

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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
  •  





Click Here to Expand Forum to Full Width

Featured