本文介绍了我怎么每次一个HTML网页浏览与网页浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有44 HTML地址,我想每一次浏览一个HTML地址。

 的for(int i = 1; I< 45;我++)
            {
                adrBarTextBox.Text = sourceUrl + I;
                。getCurrentBrowser()导航(adrBarTextBox.Text);
            }
 

在web浏览器文件完成的事件,我需要,只有当它完成了导航和加载所有包括JavaScript和一切才定位到下一个HTML地址的页面。

 私人无效Form1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
        {
          //如果完全地加载网页,然后做一些事情
        }
 

的问题是,该环路将尝试每次浏览一个新的页面的HTML最后一个被载入前,在我之前有什么事情的完成情况。

修改

按钮单击事件:

 私人无效toolStripButton3_Click(对象发件人,EventArgs的)
        {
            GetHtmls();
            CheckQueue();
        }
 

然后GetHtmls方式:

 专用队列<乌里> myUrls =新问答LT;乌里>();
        私人布尔isBusy = FALSE;

        私人无效GetHtmls()
        {
            的for(int i = 1; I< 45;我++)
            {
                adrBarTextBox.Text = sourceUrl + I;
                targetHtmls =(combinedHtmlsDir +\\的Html+ 1 +名.txt);
                乌里targetURI中=新的URI(sourceUrl + I);
                myUrls.Enqueue(targetURI中);
            }
        }
 

然后checkQueue方式:

 私人无效CheckQueue()
        {
            如果(isBusy)
                返回; //我们下载了一些网页,现在,请勿打扰

            isBusy = TRUE; // OK,让我们开始吧

            如果(myUrls.Count == 0)//没有更多的网页下载,我们就大功告成了
            {
                isBusy = FALSE;
                返回;
            }

            开放的我们的uri = myUrls.Dequeue(); //从队列中的一个URL
            。getCurrentBrowser()导航(URI);
        }
 

和最后完成事件:

 私人无效Form1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
        {
            myUrls.Dequeue();
            //如果完全地加载网页,然后做一些事情

            isBusy = FALSE; // 做完了
            CheckQueue(); //检查队列中下页
        }
 

解决方案

您正在浏览的所有网页,在同一时间,我的循环。在我看来,你需要的时候被下载一个网页,将被检查队列中。

 专用队列<乌里> myUrls =新问答LT;乌里>();
私人布尔isBusy = FALSE;
 

isBusy 标志将是我们的下一个方法,因为我们只想下载一页一次。

 私人无效CheckQueue()
{
    如果(isBusy)
        返回; //我们下载了一些网页,现在,请勿打扰

    isBusy = TRUE; // OK,让我们开始吧

    如果(myUrls.Count == 0)//没有更多的网页下载,我们就大功告成了
    {
        isBusy = FALSE;
        返回;
    }

    开放的我们的uri = myUrls.Dequeue(); //从队列中的一个URL
    webBrowser.Navigate(URI); //下载页面
}
 

我们将调用此方法,当一个页面就会被下载。

 私人无效Form1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
{
    //如果完全地加载网页,然后做一些事情

    isBusy = FALSE; // 做完了
    CheckQueue(); //检查队列中下页
}
 

当然,你得叫 CheckQueue()来初始化下载网页的过程。

I have 44 html addresses and i want each time to navigate to one html address.

for (int i = 1; i < 45; i++)
            {
                adrBarTextBox.Text = sourceUrl + i;
                getCurrentBrowser().Navigate(adrBarTextBox.Text);
            }

In the webbrowser document completed event i need that only when it finished to navigate and loading all the page including javascript and everything only then to navigate to the next html address.

private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
          // If page loaded completly then do something
        }

The problems are that the loop will try to navigate each time a new page html before the last one loaded and before i did with it something in the completed event.

EDIT

Button click event:

private void toolStripButton3_Click(object sender, EventArgs e)
        {
            GetHtmls();
            CheckQueue();
        }

Then the GetHtmls method:

private Queue<Uri> myUrls = new Queue<Uri>();
        private bool isBusy = false;

        private void GetHtmls()
        {
            for (int i = 1; i < 45; i++)
            {
                adrBarTextBox.Text = sourceUrl + i;
                targetHtmls = (combinedHtmlsDir + "\\Html" + i + ".txt");
                Uri targetUri = new Uri(sourceUrl + i);
                myUrls.Enqueue(targetUri);
            }
        }

Then the checkQueue method:

private void CheckQueue()
        {
            if (isBusy)
                return; // We're downloading some page right now, don't disturb

            isBusy = true; // OK, let's get started

            if (myUrls.Count == 0) // No more pages to download, we're done
            {
                isBusy = false;
                return;
            }

            Uri uri = myUrls.Dequeue(); // Get one URL from queue
            getCurrentBrowser().Navigate(uri);
        }

And last the completed event:

private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            myUrls.Dequeue();
            // If page loaded completly then do something

            isBusy = false; // We're done
            CheckQueue(); // Check next page in queue
        }
解决方案

You're navigating to all pages at the same time I the loop. In my opinion you need a queue which will be checked when one page is downloaded.

private Queue<Uri> myUrls = new Queue<Uri>();
private bool isBusy = false;

The isBusy flag will be for our next method, because we want to download only one page at once.

private void CheckQueue()
{
    if (isBusy)
        return; // We're downloading some page right now, don't disturb

    isBusy = true; // OK, let's get started

    if (myUrls.Count == 0) // No more pages to download, we're done
    {
        isBusy = false;
        return;
    }

    Uri uri = myUrls.Dequeue(); // Get one URL from queue
    webBrowser.Navigate(uri); // Download the page
}

We will be calling this method when one page will be downloaded.

private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // If page loaded completly then do something

    isBusy = false; // We're done
    CheckQueue(); // Check next page in queue
}

And of course you have to call CheckQueue() to initialize the process of downloading pages.

这篇关于我怎么每次一个HTML网页浏览与网页浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 16:50
查看更多