C# 如何等待网站加载完成?

C# 如何等待网站加载完成?,c#,.net,winforms,C#,.net,Winforms,我的代码现在 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Web.UI; namespa

我的代码现在

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web.UI;

namespace myweb
{
    public partial class Form1 : Form
    {
        static Page page;

        public Form1()
        {
            InitializeComponent();

            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8");

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string test "completed";
        }
    }
}
问题在于,它在加载自己完成的页面之前,多次到达DocumentCompleted事件。我在string test=“completed”上使用了断点;在页面完全加载之前,它在那里停了几次

我希望它将到达那里,并使行字符串测试=“已完成”;最后,当网页完全加载时,只需执行一次操作。

尝试以下操作:

public Form1()
{
   InitializeComponent();

   webBrowser1.ScriptErrorsSuppressed = true;
   webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8");
   while(webBrowser1.ReadyState != WebBrowserReadyState.Complete)
   {
        Application.DoEvents();
   }
   MessageBox.Show("Site Loaded");
}
试试这个:

public Form1()
{
   InitializeComponent();

   webBrowser1.ScriptErrorsSuppressed = true;
   webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393/%D7%98%D7%91%D7%A2_%D7%95%D7%9E%D7%96%D7%92_%D7%90%D7%95%D7%95%D7%99%D7%A8/%D7%9E%D7%96%D7%92_%D7%94%D7%90%D7%95%D7%95%D7%99%D7%A8");
   while(webBrowser1.ReadyState != WebBrowserReadyState.Complete)
   {
        Application.DoEvents();
   }
   MessageBox.Show("Site Loaded");
}

每次加载帧时,都会触发事件

DocumentComplete
可能由于多种原因(帧、ajax等)而被多次解雇。同时,对于特定文档,
window.onload
事件将只触发一次。因此,也许您可以在
window.onload
中进行处理。我在下面试过了。希望能有帮助

private void Form1_Load(object sender, EventArgs e){
bool complete = false;
this.webBrowser1.DocumentCompleted += delegate
{
    if (complete)
        return;
    complete = true;
    // DocumentCompleted is fired before window.onload and body.onload
    this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
    {
        // Defer this to make sure all possible onload event handlers got fired
        System.Threading.SynchronizationContext.Current.Post(delegate 
        {
            // try webBrowser1.Document.GetElementById("id") here
            MessageBox.Show("window.onload was fired, can access DOM!");
        }, null);
    });
};

this.webBrowser1.Navigate("http://www.example.com");}

每次加载帧时,都会触发事件

DocumentComplete
可能由于多种原因(帧、ajax等)而被多次解雇。同时,对于特定文档,
window.onload
事件将只触发一次。因此,也许您可以在
window.onload
中进行处理。我在下面试过了。希望能有帮助

private void Form1_Load(object sender, EventArgs e){
bool complete = false;
this.webBrowser1.DocumentCompleted += delegate
{
    if (complete)
        return;
    complete = true;
    // DocumentCompleted is fired before window.onload and body.onload
    this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
    {
        // Defer this to make sure all possible onload event handlers got fired
        System.Threading.SynchronizationContext.Current.Post(delegate 
        {
            // try webBrowser1.Document.GetElementById("id") here
            MessageBox.Show("window.onload was fired, can access DOM!");
        }, null);
    });
};

this.webBrowser1.Navigate("http://www.example.com");}