I'm really having a big issue with the Socket.Connect() method locking up all the threads on the Netduino, when the server isn't available. I've read a few articles about this, but no matter how many threads I create the connection on, the Connect() line stops everything from happening.
I've created a thread that monitors the connection and creates a new one if it's not up. I use this to reconnect automatically if something goes wrong.
Has anyone experienced this and solved it? I'm running v4.1.2 firmware.
Connection thread:
ThreadState state = ThreadState.Running; Thread conThread = null; while (state == ThreadState.Running) { Debug.Print("Attempting connection..."); if (conThread != null) conThread.Abort(); conThread = new Thread(CreateConnection); conThread.Start(); Thread.Sleep(500); // Wait 500ms for conThread to end, or continue looping until connected (hopefully?) state = conThread.ThreadState; }
The thread that creates the actual connection. It stops all threads from running once I start it (if no network cable is plugged in)
public void CreateConnection() { try { Server.Connect(new IPEndPoint(IPAddress.Parse(Address), Port)); //Thread.CurrentThread.Abort(); // A signal that we're connected (see for loop on owner thread) } catch (SocketException ex) { Debug.Print(ex.Message); }