MicrosoftEdge / MicrosoftEdge/WebView2Samples

doubt to synchronize!

Open
#21 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
1k
Forks
521
Avg merge
13m
Merged PRs (30d)
1

Description

I tried to do some ways to synchronize Callback (NavigationCompleted), but I was unsuccessful.

For example, I want to execute (ExecuteScript or PostWebMessageAsString). But I need to make sure the page loads before! I know that AddScriptToExecuteOnDocumentCreated exists and I could use it, but it's not just in the creation, in some moments if I need to run a script I have no way of knowing if the full navigation was done.

One of the examples I tested:

webviewWindow->add_NavigationCompleted(Callback<IWebView2NavigationCompletedEventHandler>(
	[&](IWebView2WebView* webview, IWebView2NavigationCompletedEventArgs * args) -> HRESULT {	
	SetEvent(NavigationCompletedEvent.Get());
	return S_OK;
}).Get(), &token);

ResetEvent(NavigationCompletedEvent.Get());

webviewWindow->Navigate(GetFullPathFor(L"htmls\\MyAppWeb.htm"));

WaitForSingleObjectEx(NavigationCompletedEvent.Get(), INFINITE, FALSE);

// Scripting Check Page HTML is READ to CLOSE!
webviewWindow->ExecuteScript(L"window.fnIsReadyToClose();", Callback<IWebView2ExecuteScriptCompletedHandler>(
	[](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
	LPCWSTR IsReadyToClose = resultObjectAsJson;
	//Close Window or change page etc...	
}).Get());

I tried how to run with Navigate with RunAsync (using POSTMESSAGE), but nothing!

From what I saw it is on the same thread and does not run async on another! I also didn't see anything like std :: future using a myFuture.wait (); I tried with CreateEvent and condition_variable, but it ends up the same.

I don't know if it's the best way! What do you suggest?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the shown NavigationCompleted callback and the Navigate, ExecuteScript, and PostWebMessageAsString calls. Compare their callback and threading behavior in the WebView2 samples or API documentation, then define a reproducible navigation-and-scripting sequence and the expected completion point.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
desktop
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.