Associate the handler to the main thread

Previous implementation associate the handler to the current thread,
which can not be the main thread.
Merge pull request #694 from tbruyelle/693_HandlerInMainThread

Associate the handler to the main thread