private void Form1_FormClosed(object sender, FormClosedEventArgs e) { try { if (comPort.IsOpen) { port.Close(); } } catch { } Application.Exit(); }