C# Winforms -> How to have execution wait until page load complete?

267 views
Skip to first unread message

Shaareable Apps

unread,
May 26, 2020, 10:05:10 PM5/26/20
to CefSharp
Greetings,

I'm writing an app for my wife that has to spends countless hours everyday struggling with a primitive, ugly website designed in the 90's.(consultant page for Usborne's kids books).
The goal is to automate much of the drudgery. Everything is going smoothly so far, I can scrape the sucker, but there's one key thing I'm missing.

How do we know when the page load is finished?

I once code an excel vba app and that ie programming was simple enough.

Please tell me we have a way here to get the status 200! :)

Thank you kindly from Vancouver, :)

Antoine

Ed. M.

unread,
Jun 4, 2020, 2:06:42 PM6/4/20
to CefSharp
Here's what I do.  I setup the LoadingStateChanged event:
_browser.LoadingStateChanged += BrowserLoadingStateChanged;

Then in the handler, I notify that the page as finished loading:
private static void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
_tcs?.TrySetResult(true);
}
}

tcs is a TaskCompletionSource that both the handler and my code and see:
private static TaskCompletionSource<bool> _tcs = null;

Then in the code, once I call EvaluateScriptAsync, I then do:
_tcs = new TaskCompletionSource<bool>();
await _tcs.Task;

which will cause the main thread to wait.

Hope this helps.
 - Ed.
Reply all
Reply to author
Forward
0 new messages