我在做webbrowser的时候需要判断网页是否加载完成。[C#] 纯文本查看 复制代码 private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (webBrowser1.DocumentTitle.ToString().Length > 20) //判断网页标题是否大于20字节
{
tabPage1.Text = webBrowser1.DocumentTitle.ToString().Substring(0, 20) + "……";
this.Text = webBrowser1.DocumentTitle.ToString().Substring(0, 20) + "……" + webBrowser1.DocumentTitle.ToString().Substring(webBrowser1.DocumentTitle.ToString().Length - 10, 10) + "---Windows Internet Explorer";
}
else
{
tabPage1.Text = webBrowser1.DocumentTitle.ToString();
this.Text = webBrowser1.DocumentTitle.ToString() + "---Windows Internet Explorer";
}
progressBar1.Visible = true;
toolStripButton1.Enabled = true;
返回ToolStripMenuItem.Enabled = true;
comboBox1.Text = webBrowser1.Url.ToString();
if ((e.CurrentProgress > 0) && (e.MaximumProgress > 0)) //判断已下载字节数与总字节数是否大于0
{
toolStripStatusLabel1.Text = "等待:" + webBrowser1.Url.ToString();
progressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
progressBar1.Step = Convert.ToInt32(e.CurrentProgress);
progressBar1.PerformStep();
}
else
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)//判断网页文档是否加载完
{
progressBar1.Value = 0;
progressBar1.Visible = false;
toolStripStatusLabel1.Text = "完成";
okflag = true;
fzflag = true;
}
}
我首先运行这段,然后调上面的函数
[C#] 纯文本查看 复制代码 webBrowser1.Navigate(doc.Url);
okflag = false;//判断当前frame加载完成
bool flag = false;//当前页面完全加载完成
while (!flag && !stopcapture)
{
timer2.Interval = 10000;//设置一个frame加载时间不超过10秒,否则直接跳出while循环
timer2.Start();
while (!okflag && timeflag && !stopcapture)
{
Application.DoEvents();
}
timer2.Stop();
问题是 在Application.DoEvents();的时候,程序飞了,okflag已经为true了,无法跳出while循环。
|