我有这段代码,可以轻松导航到网站
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
}
}
如果创建一个browserurl对象,一切正常,但是如果创建两个对象,似乎两个构造函数同时运行,那么我如何让第二个对象等到第一个结束执行
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);
}
}
将in的值设置_isLoaded
为true
in,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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句