-
November 21st, 2010, 06:42 AM
#1
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+ "¶m2=" + 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.
-
November 23rd, 2010, 09:30 AM
#2
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.
-
November 23rd, 2010, 10:02 AM
#3
Re: Why this thread exits?
Originally Posted by boudino
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.
-
November 24th, 2010, 02:20 AM
#4
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.
-
November 24th, 2010, 03:52 AM
#5
Re: Why this thread exits?
Originally Posted by boudino
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!
-
November 24th, 2010, 01:57 PM
#6
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.
-
November 24th, 2010, 06:26 PM
#7
Re: Why this thread exits?
Originally Posted by Mutant_Fruit
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|