Skip to content

doubt to synchronize! #21

@rflsouza

Description

@rflsouza

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions