简体   繁体   中英

C# how to wait for a webpage to finish loading

Hi I am trying to find a method of waiting a number of milliseconds before moving to the next line of code,

I have looked into Thread.Sleep but this will freeze the main form, I would like this to remain active

Import this package below

using System.Threading.Tasks;

If you are using .Net framework 4.5 then can use Task.Delay

await Task.Delay(2000);

You dont have to use Thread.sleep.

If you are trying to do this because you are working directly with a browser, threading will be a nightmare, if you have a browser control then this is what your actually looking for:

(wb is the browser control in this instance)

while(wb.ReadyState != WebBrowserReadyState.Complete)
{
     Application.DoEvents();
}

This is used when navigating to wait for the page to load, anything after this only starts once the page has progressed. I would recommend spoofing the server requests so you dont have to worry about javascript etc but if the site is simple this will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM