Check for command cancellation before the loop