Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webbrowser执行流_C# - Fatal编程技术网

C# webbrowser执行流

C# webbrowser执行流,c#,C#,我有这个代码,它可以简单地导航到一个网站 class BrowsingUrl { private System.Windows.Forms.WebBrowser nBrowser ; private System.Windows.Forms.Form theFormLayout1; public BrowsingUrl(System.Windows.Forms.Form theFormLayout) { this.nBrowser =

我有这个代码,它可以简单地导航到一个网站

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
   }
}    
如果创建一个browsingurl对象,一切正常,但是如果创建两个对象,两个构造函数似乎同时运行,我怎么能让第二个对象等待第一个一结束执行呢

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);
        }
}    

nBrowser\u DocumentCompleted
中,将
\u isLoaded
的值设置为
true
,并将其作为公共属性进行访问。同时添加
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;
        }
    }  
然后您可以等待,直到加载
浏览URL1

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

不幸的是,可能重复出现不同的问题我想@magicalll需要等待第一个
nBrowser\u DocumentCompleted
完成,然后再启动第二个构造函数。是的@kennyzx这正是我要找的谢谢先生,我尝试了这个解决方案,但没有运气两个实例都在运行,但是这个解决方案让我明白了,这两个实例是在两个独立的线程中运行的,是的,我现在看到了,它在某些页面上不起作用。问题是页面中存在异步执行的javascript。“我会尝试找到其他方法。”magicalll更新了答案,经过测试,似乎效果不错。但是,您的问题可能是您希望访问加载文档中的一些HTML(“代码”)并希望访问修改后的HTML。这将是一个不同的问题。是的,你完全正确,如果我试图在nBrowser_文档中“做一些事情”,就会出现你提到的问题。否则,解决方案是完美的,但如果我可以问你最后一个问题,你会建议用什么方法来操纵dom@magicalll我使用Selenium Web驱动程序,或者你可能会抱怨。这些是用于单元测试web页面(甚至包含AJAX等)的工具