检查多个线程中的URL

时间:2017-07-11 07:30:08

标签: c# .net multithreading backgroundworker system.web

我正在尝试使用以下代码验证网址列表

class MyClient : WebClient
{
    public bool HeadOnly { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest req = base.GetWebRequest(address);

        if (HeadOnly && req.Method == "GET")
        {
            req.Method = "HEAD";
        }
        return req;
    }
}

private static Boolean CheckURL(string url)
{
    using (MyClient myclient = new MyClient())
    {
        try
        {
            myclient.HeadOnly = true;
            // fine, no content downloaded
            string s1 = myclient.DownloadString(url);
            return true;
        }
        catch (Exception error)
        {
            return false;
        }
    }
}

这很有效,但对于某些网站来说,它需要等待才能获得结果。如何将URL列表拆分为2个或4个部分并使用单独的线程验证每个部分并将进度报告给单个进度条?

请建议。

更新

我正在使用Backgroundworker验证URl

private void URLValidator_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (var x in urllist)
    {         
        Boolean valid = CheckURL(x);
    }
}

1 个答案:

答案 0 :(得分:3)

您可以使用Parallel.ForEach

using System.Threading.Tasks;

private void URLValidator_DoWork(object sender, DoWorkEventArgs e)
{
    Parallel.ForEach(urllist, (url) =>
    {
        Boolean valid = CheckURL(x);
        // Do something with the result or save it to a List/Dictionary or ...
    });
}