Function waitForClosed