The best way to handle this is for the thead with the timer to call a stop method in the class where the socket receive method is. This class will have a AutoResetEvent member instance and in the while for the socket read check the event to see if you need to stop:
public class SomeClass {
AutoResetEvent _stopEvent = new AutoResetEvent(false);
Socket _socket;
public void StopReceive() {
_stopEvent.Set();
}
private void SomeMethod() {
_socket.ReceiveTimeout = 5000; // In milliseconds
while(!_stopEvent.WaitOne(0)){
_socket.Receive(buffer);
}
}
}
Edit:
If you have two sockets for the two players on two different threads, with the third thread handling the timeout then the code in all three threads need access to the same event, and change the event to a ManualResetEvent. What I am not sure about is if you have one class that handles the timer thread, and another class that handles the socket read threads for the two players, or if they are handled by one class. If they are separate classes then for the above code you could pass the event in the constructor:
public class SomeClass {
ManualResetEvent _stopEvent;
Socket _socket;
public SomeClass(ManualResetEvent stopEvent) {
_stopEvent = stopEvent;
}
private void SomeMethod() {
_socket.ReceiveTimeout = 5000; // In milliseconds
while(!_stopEvent.WaitOne(0)){
_socket.Receive(buffer);
}
}
}
The timer thread could now call the Set() method in the ManualResetEvent instance and both while loops would terminate.