private void Form1_FormClosed(object sender, FormClosedEventArgs e) { phidgetReciver.Stop(); arduinoReciver.Stop(); arduinoSender.Stop(); motionCalculator.Stop(); dataExchanger.Stop(); phidgetReciver.abort = true; arduinoReciver.abort = true; arduinoSender.abort = true; motionCalculator.abort = true; dataExchanger.abort = true; udpReceiver.Stop(); abort = true; if(PhidgetThread.IsAlive)PhidgetThread.Join(); if(ReciverThread.IsAlive)ReciverThread.Join(); if(SenderThread.IsAlive)SenderThread.Join(); if(CalculatorThread.IsAlive)CalculatorThread.Join(); //if (UDPReceiverThread.IsAlive) UDPReceiverThread.Join(); for (int j = 0; j < listofPorts.Count; j++) { for (int i = 0; i < 6; i++) { string tmp = ("{M" + i + ",F,0}"); listofPorts[j].Write(tmp); listofPorts[j].ReadExisting(); } } Thread.Sleep(100); for (int i = 0; i < listofPorts.Count; i++) { listofPorts[i].Close(); } }