Associate the handler to the main thread

Previous implementation associate the handler to the current thread,
which can not be the main thread.