【问题标题】:C# stopping an infinite foreach loopC# 停止无限的 foreach 循环
【发布时间】:2013-07-27 08:00:22
【问题描述】:

此 foreach 循环检查网页并查看是否有任何图像然后下载它们。我该如何阻止它?当我按下按钮时,它会永远继续循环。

 private void button1_Click(object sender, EventArgs e)
    {
        WebBrowser browser = new WebBrowser();
        browser.DocumentCompleted +=browser_DocumentCompleted;
        browser.Navigate(textBox1.Text);           
    }

    void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser = sender as WebBrowser;
        HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
        WebClient webClient = new WebClient();

        int count = 0; //if available
        int maximumCount = imgCollection.Count;
        try
        {
                foreach (HtmlElement img in imgCollection)
                {
                    string url = img.GetAttribute("src");
                    webClient.DownloadFile(url, url.Substring(url.LastIndexOf('/')));

                     count++;
                     if(count >= maximumCount)
                          break;
                }
        }
        catch { MessageBox.Show("errr"); }
    }

【问题讨论】:

  • 你说的“按钮”是什么?
  • 你想什么时候停止?
  • 这在我看来不像是无限循环......
  • 你能贴出imgCollection的填充代码吗?
  • 你确定这不只是需要很长时间吗?你试过断点吗?

标签: c# foreach


【解决方案1】:

使用break; 关键字跳出循环

【讨论】:

  • 我如何知道何时使用break;
  • 如果你不知道什么时候停止,那么你必须找出为什么循环是无限的。如果你保持迭代计数,你可以选择任意数量的迭代,然后爆发。
【解决方案2】:

你没有无限循环,你有一个基于你如何将文件写入磁盘而引发的异常

private void button1_Click(object sender, EventArgs e)
{
    WebBrowser browser = new WebBrowser();
    browser.DocumentCompleted += browser_DocumentCompleted;
    browser.Navigate("www.google.ca");
}

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
    WebClient webClient = new WebClient();

    foreach (HtmlElement img in imgCollection)
    {
        string url = img.GetAttribute("src");
        string name = System.IO.Path.GetFileName(url);
        string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
        webClient.DownloadFile(url, path);
    }
}

该代码在我的环境中运行良好。您似乎遇到的问题是,当您设置 DownloadFile filepath 时,您将其设置为类似 `\myimage.png' 的值,而网络客户端找不到路径,因此抛出异常。

上面的代码用扩展名把它放到当前目录中。

【讨论】:

  • 你得到的最大数量是多少?
  • 可以得到完整的按钮方法吗?
【解决方案3】:

可能是事件 browser.DocumentCompleted 导致错误,如果页面刷新,事件会再次触发。您可以尝试注销该事件。

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{    
    WebBrowser browser = sender as WebBrowser;

    browser.DocumentCompleted -= browser_DocumentCompleted;

    HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
    WebClient webClient = new WebClient();

    foreach (HtmlElement img in imgCollection)
    {
        string url = img.GetAttribute("src");
        string name = System.IO.Path.GetFileName(url);
        string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
        webClient.DownloadFile(url, path);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2014-08-26
    • 2021-10-21
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多