CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2005
    Location
    .NET 3.5
    Posts
    97

    Why this thread exits?

    In a thread different from the main one, i am sending/receiving data to a socket and i am waiting the response via a callback, but the thread enters the "Stopped" state even if i'm waiting for pending operations to end, and this is strange because the callback functions are still executed on that thread, while it is "Stopped"!!

    This is the main thread, where i create the new thread

    Code:
                                                        // BeginRequestToMainServer defined later
                Thread t = new Thread(BeginRequestToMainServer); 
                t.Start();
    
                t.Join();
    
                    /* when this Messagebox prints "Stopped", the callback functions on the t thread
                        keep executing!
                    */
               MessageBox.Show("t state is " + t.ThreadState);
    This is the separate thread:

    Code:
            private void BeginRequestToMainServer()
            {
                try
                {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.BeginConnect(mainserveraddress, 80, new AsyncCallback(OnConnectToMainServer), null);
                }
                catch (Exception ex)
                {                
                    socket.Close();
                }
    
              /*
                 Here the thread procedure returns, and then the thread turns into the Stopped state, 
                 but this should not happen because there is a pending asynchronous operation 
                 in this thread!
                 Indeed, the OnConnectToMainServer callback will be called later
             */
    
            } // end BeginRequestToMainServer
     
    
    
                /*
                    OnConnectToMainServer will be executed even if the main thread has already exited
                    the t.Join() call, and this thread is in the Stopped state
                */
            protected override void OnConnectToMainServer(IAsyncResult res)
            {
                // send post parameters to the page
                String vars = "param1=" + param1+ "&param2=" + param2;
                String postrequest = "POST /page.php HTTP/1.1\r\n" +
                                     "Host: " + mainserveraddress + "\r\n" +
                                     "Content-Length: " + vars.Length + "\r\n" +
                                     "Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
                                     vars;
    
                try
                {
                    socket.BeginSend(Encoding.ASCII.GetBytes(postrequest), 0, postrequest.Length, SocketFlags.None, new AsyncCallback(OnSendToMainServer), null);
                }
                catch (Exception ex)
                {
                    socket.Close();
                }
    
            } // end OnConnectToMainServer
    
    
                /*
                    OnSendToMainServer will be executed even if the main thread has already exited the
                    t.Join() call, and this thread is in the Stopped state
                */
        protected override void OnSendToMainServer(IAsyncResult res){
            ........
        }
    What is going on here?
    That tread is dead but it executes the callbacks :O

    At the end, what i want is to block the main thread until the other one ends the communication via the socket
    Last edited by GordonFreeman; November 21st, 2010 at 06:45 AM.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Why this thread exits?

    Because in BeginRequestToMainServer you call Socket.BeginConnect(), which begins an asynchronous request for a remote host connection. That means, that call to BeginConnect() returns immediately which causes that BeginRequestToMainServer() returns immediately too and calling to Join() ends, displaying the message "Stopped".
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Mar 2005
    Location
    .NET 3.5
    Posts
    97

    Re: Why this thread exits?

    Quote Originally Posted by boudino View Post
    Because in BeginRequestToMainServer you call Socket.BeginConnect(), which begins an asynchronous request for a remote host connection. That means, that call to BeginConnect() returns immediately which causes that BeginRequestToMainServer() returns immediately too and calling to Join() ends, displaying the message "Stopped".

    ok ok i see: the Stopped state does not mean "the thread exited" , it is still alive but not executing the main procedure.
    so the Join() returns after the thread procedure ends and not necessaily if the thread ends.

    i solved using a Semaphore to force the thread procedure BeginRequestToMainServer to wait before returning to the caller, until the async callbacks end communicating via the socket.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Why this thread exits?

    The thread ends too, but BeginConnect() starts a new one. That's the point. To wait for it, use something like
    Code:
    IAsyncResult ar = socket.BeginConnect(...);
    ar.WaitHandle.WaitOne();
    Last edited by boudino; November 24th, 2010 at 02:24 AM.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Mar 2005
    Location
    .NET 3.5
    Posts
    97

    Re: Why this thread exits?

    Quote Originally Posted by boudino View Post
    The thread ends too, but BeginConnect() starts a new one. That's the point. To wait for it, use something like
    Code:
    IAsyncResult ar = socket.BeginConnect(...);
    ar.WaitHandle.WaitOne();
    i didn't know that, thank u!

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Why this thread exits?

    Or just use socket.Connect (), it is the synchronous version
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Mar 2005
    Location
    .NET 3.5
    Posts
    97

    Re: Why this thread exits?

    Quote Originally Posted by Mutant_Fruit View Post
    Or just use socket.Connect (), it is the synchronous version
    As far as i know synchronous socket calls are not supported since .NET ver. 2.. or maybe that's true for Silverlight only ??

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