I have to write an C# API for registering global hotkeys. To receive the WM_HOTKEY message, I use a System.Windows.Forms.NativeWindow
and run an own message loop with System.Windows.Forms.Application.Run(ApplicationContext)
. When the user wants to register a hotkey, he has to run a method called RegisterHotkey()
which stops the message loop with System.Windows.Forms.ApplicationContext.ExitThread()
, registers the hotkey with the RegisterHotKey()
(P/Invoke) function and starts the message loop again. This is required, because RegisterHotKey()
must be called within the same thread that created the window, which again must be instantiated within the same thread that runs the message loop.
The problem is, that if the user calls the RegisterHotkey()
method shortly after starting the thread which is running the message loop, ApplicationContext.ExitThread()
gets called before Application.Run(ApplicationContext)
and therefore the application blocks indefinitely. Does anybody know an approach for waiting for a message loop to be started?
Thanks in advance!
NativeWindow.CreateHandle(CreateParams)
instead. – Rashida