So, we need a method that returns a task when the DocumentCompleted event fires. At any time, when it is necessary for this event, you can create such a method:
public static Task WhenDocumentCompleted(this WebBrowser browser) { var tcs = new TaskCompletionSource<bool>(); browser.DocumentCompleted += (s, args) => tcs.SetResult(true); return tcs.Task; }
After that you can use:
await browser.WhenDocumentCompleted();
Servy source share