Notify the main GUI thread upon receiving a close event instead of when the windows is destroyed. Additionally there was a logic error in in GuiReceiver::isLastWindow() that is corrected. Fixes #6479 and #20822