Running host objects on a different thread to avoid hanging app? #2916
-
Hopefully I'm missing something obvious, but is there a way to avoid blocking the main thread when executing host object methods/getters? What I've triedI'm aware that host objects are exposed via I'm also aware that there is a I can't internally farm the work out to another thread because the host object is synchronous, and it's not possible (as far as I'm aware) to return a promise object directly from my native code. A bad workaroundObviously, I could implement some sort of polling, but I'd like to expose a promise-based interface. This doesn't seem possible given the above. QuestionsAny other ideas? Is there an option I need to provide to WebView2 to let it run my host object on its own thread? The threading model doc mentions "The WebView2 must be created on a UI thread that uses a message pump. All callbacks occur on that thread", but there is no mention of whether all host object proxy calls also must happen on that same thread (although that's how it appears to me). ReferencesI've read through these pages and am still not seeing a solution:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I'm starting to suspect that the async host object proxies only exist because JavaScript code and the thread that created the WebView2 are running in different contexts and that the async proxies aren't designed to support slow methods (like I had hoped). If that's correct, then I guess I will need to create my own async return mechanism (probably based on Edit: After thinking about this some more, it may be possible to pass the |
Beta Was this translation helpful? Give feedback.
-
I never found any built-in support for exposing async methods on host objects that actually run async, so... My solution was to move methods that could be slow to a thread pool and then expose everything as a JavaScript promise by passing |
Beta Was this translation helpful? Give feedback.
-
Update 2024: Direct support for calling async methods on host objects has been added to the SDK (.NET, WinRT): #75 (comment) |
Beta Was this translation helpful? Give feedback.
I never found any built-in support for exposing async methods on host objects that actually run async, so...
My solution was to move methods that could be slow to a thread pool and then expose everything as a JavaScript promise by passing
resolve
andreject
fromnew Promise((resolve, reject) => ...
to my native code and marshal the IDispatch interfaces over to the thread pool thread where they eventually get called to resolve or reject the promise.