Added an exception checking to determine if the message queue was closed by the main thread