I have a wpf program where i want to add a WebBrowser control
I search how to check if the WebBrowser loaded the page successfully or not and here is the code:
private async Task PageLoad(int TimeOut)
{
TaskCompletionSource<bool> PageLoaded = null;
PageLoaded = new TaskCompletionSource<bool>();
int TimeElapsed = 0;
webBrowser.LoadCompleted += (s, e) =>
{
if (webBrowser.ReadyState != webBrowser.Complete) return;
if (webBrowser.Task.IsCompleted) return;
PageLoaded.SetResult(true);
};
while (PageLoaded.Task.Status != TaskStatus.RanToCompletion)
{
//interval of 10 ms worked good for me
await Task.Delay(10);
TimeElapsed++;
if (TimeElapsed >= TimeOut * 100)
{
//This prevents your method or thread from waiting forever
PageLoaded.TrySetResult(true);
}
}
}
But why it doesn't work on wpf. How do I convert it?
I check it and the ReadyState
Complete
and Task.IsCompleted
don't have a property like that in wpf. What is the code for wpf?