private void Main_FormClosing(object sender, FormClosingEventArgs e) { if (connection != null) { connection.Close(); connection = null; command = null; reader = null; } Application.ExitThread(); }