public void Receive(SystemMessage message) { if (message.ChannelName == SystemMessaging.CHANNEL) { SystemMessaging.MessageType messageType = (SystemMessaging.MessageType)message.MessageType; ISystemStateService sss = ServiceRegistration.Get <ISystemStateService>(); if (messageType == SystemMessaging.MessageType.SystemStateChanged) { if (sss.CurrentState == SystemState.ShuttingDown || sss.CurrentState == SystemState.Ending) { // It is necessary to block the main thread until our message delivery thread has delivered all pending // messages to avoid asynchronous threads during the shutdown phase. // It's a bit illegal to call the Shutdown() method which acquires a lock in this synchronous message // handler method. But as we know that the SystemStateChanged message is only called by our application // main thread which doesn't hold locks, this won't cause deadlocks. // How ugly, but that's life. _owner.Shutdown(); } } } }