webbrowser执行流程

时间:2014-09-28 15:04:17

标签: c#

我有这个代码,只需导航到一个网站

class BrowsingUrl {
    private System.Windows.Forms.WebBrowser nBrowser ;
        private  System.Windows.Forms.Form theFormLayout1;

    public BrowsingUrl(System.Windows.Forms.Form theFormLayout) {

            this.nBrowser = new System.Windows.Forms.WebBrowser();
            this.nBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nBrowser.Location = new System.Drawing.Point(0, 0);
            this.nBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.nBrowser.Name = "webBrowser1";
            this.nBrowser.ScrollBarsEnabled = false;
            this.nBrowser.Size = new System.Drawing.Size(1300, 700);
            this.nBrowser.TabIndex = 0;
            this.theFormLayout1 = theFormLayout;
            this.theFormLayout1.Controls.Add(this.nBrowser);
            this.nBrowser.Navigate("https://stackoverflow.com");
            this.nBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.nBrowser_DocumentCompleted);
   }

   private void nBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
   {

        // do stuff
   }
}    

如果创建一个browseurl对象一切正常但是如果创建两个对象似乎是两个构造函数同时运行,我怎么能让第二个对象等到第一个结束执行结束

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }

        private void startbtn_Click(object sender, EventArgs e)
        {
            BrowsingUrl BrowsingUrl1 = new BrowsingUrl(this);
            //wait till browsingUrl1 finish
            BrowsingUrl BrowsingUrl2 = new BrowsingUrl(this);
        }
}    

1 个答案:

答案 0 :(得分:0)

_isLoaded中将true的值设置为nBrowser_DocumentCompleted,并将其作为公共属性进行访问。还要添加ScriptErrorsSuppressed = true

    class BrowsingUrl
    {
        private System.Windows.Forms.WebBrowser nBrowser;
        private System.Windows.Forms.Form theFormLayout1;
        private bool _isLoaded = false;
        public bool IsLoaded
        {
            get { return _isLoaded && !nBrowser.IsBusy && 
                  nBrowser.ReadyState == WebBrowserReadyState.Complete; }
        }
        public BrowsingUrl(System.Windows.Forms.Form theFormLayout)
        {

            this.nBrowser = new System.Windows.Forms.WebBrowser();
            this.nBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nBrowser.Location = new System.Drawing.Point(0, 0);
            this.nBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.nBrowser.Name = "webBrowser1";
            this.nBrowser.ScrollBarsEnabled = false;
            this.nBrowser.Size = new System.Drawing.Size(1300, 700);
            this.nBrowser.TabIndex = 0;
            this.theFormLayout1 = theFormLayout;
            this.theFormLayout1.Controls.Add(this.nBrowser);
            this.nBrowser.ScriptErrorsSuppressed = true;
            this.nBrowser.Navigate("https://stackoverflow.com");
            this.nBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.nBrowser_DocumentCompleted);
        }
        private void nBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //check for frames
            if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
                return;

            // do stuff
            //Sample do stuff
            Thread.Sleep(10000);
            // end do stuff

            _isLoaded = true;
        }
    }  

然后你可以等到BrowsingUrl1加载:

        BrowsingUrl BrowsingUrl1 = new BrowsingUrl(this);
        //Wait for the first page to load
        while (!BrowsingUrl1.IsLoaded)
        {
            Application.DoEvents();
        }
        BrowsingUrl BrowsingUrl2 = new BrowsingUrl(this);