Implemented a better solution using SynchronizationContext.