gpt4 book ai didi

c# - 网页浏览器执行流程

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:27 25 4
gpt4 key购买 nike

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

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_DocumentCompleted 中将 _isLoaded 的值设置为 true 并使其作为公共(public)属性可访问。同时添加 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);

关于c# - 网页浏览器执行流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26086237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com