Why is WebViewControlProcess.CreateWebViewControlAsync() never completing?
It turned out that the problem was threading-related: the winit crate was doing its event loop in a different thread, and I did not realise this; I had erroneously assumed winit to be a harmless abstraction, which it turned out not quite to be.
I discovered this when I tried minimising and porting a known-functioning C++ example, this time doing all the Win32 API calls manually rather than using winit, so that the translation was correct. I got it to work, and discovered this:
The IAsyncOperation
is fulfilled in the event loop, deep inside a DispatchMessageW
call. That is when the Completion
handler is called. Thus, for the operation to complete, you must run an event loop on the same thread. (An event loop on another thread doesn’t do anything.) Otherwise, it stays in the Started
state.
Fortunately, winit is already moving to a new event loop which operates in the same thread, with the Windows implementation having landed a few days ago; when I migrated my code to use the eventloop-2.0 branch of winit, and to using the Completed
handler instead of blocking_get()
, it all started working.
I shall clarify about the winrt crate’s blocking_get()
call which would normally be the obvious solution while prototyping: you can’t use it in this case because it causes deadlock, since it blocks until the IAsyncOperation
completes, but the IAsyncOperation
will not complete until you process messages in the event loop (DispatchMessageW
), which will never happen because you’re blocking the thread.