I have the following:
ThreadStart startThread =
delegate
{
mySocket.StartListen();
};
mySocket is now looping on a Listen() when I:
new Thread(startThread).Start();
Here is StartListen:
public void StartListen()
{
Object locker = new Object();
// Lock resources
lock (locker)
{
S = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
S.Blocking = false;
try
{
S.Bind(serverEP);
S.Listen(1000);
isListening = true;
/*
* While statement not required here because AcceptConnection()
* method instructs the socket to BeginAccept() again...
*/
//while (true)
//{
connectDone.Reset();
S.BeginAccept(new AsyncCallback(AcceptConnection), Channel);
connectDone.WaitOne();
//}
}
catch (System.Net.Sockets.SocketException SockEx)
{
Console.WriteLine("*****[" + name + "] " + SockEx.SocketErrorCode + ":");
Console.WriteLine("*****[" + name + "] " + SockEx.Message);
}
}
}
Because of the asynchronous methods things don't really 'finish' and return a signal for anything else to continue. Any commands I implement after the Thread.Start() above are not working properly. For instance, in StartListen, note that I have an isListening = true
. After I start the thread, I want to use the property IsListening. It is always returned as false.
How should I be starting the Thread. Would an asynchronous method (i.e. ThreadStart.BeginInvoke()) be preferrable? Isn't that similar to using a ManualResetEvent?
Regards... David