在调用IProgress.Report

时间:2018-07-16 09:44:28

标签: c# asynchronous

当我调用IProgress.Report时,没有调用传递给Progress对象的构造函数的回调。在我的特定情况下,我的回调只是调用Console.WriteLine

public async Task DoWorkAsync()
{
    var driver = SeleniumUtility.CreateFirefoxDriver(@"C:\Users\david\source\repos\HelperLib\HelperLib\bin\Debug\drivers");
    driver.Navigate().GoToUrl("https://www.cool-proxy.net/proxies/http_proxy_list/sort:score/direction:desc");
    var progress = new Progress<string>(p => Console.WriteLine(p));
    await Scraper.ScrapeAsync(driver, RegexUtility.IPv4AndPortWithSeperatorRegexString, By.CssSelector("#main > table > tbody > tr:nth-child(23) > th > span.next > a"), progress);
}

public class Scraper
{
    public static async Task ScrapeAsync(IWebDriver driver, string regex, By nextPageButtonSelector, IProgress<string> progress, int nextPageButtonWaitTimeout = 20)
    {
        var matches = new List<Match>();
        do
        {
            var mc = Regex.Matches(driver.PageSource, regex);
            foreach (Match m in mc)
            {
                matches.Add(m);
            }
            progress.Report($"Found {mc.Count} matches on {driver.Url}");
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(nextPageButtonWaitTimeout));
            wait.Until(ExpectedConditions.ElementIsVisible(nextPageButtonSelector));
            var nextPageButton = driver.FindElement(nextPageButtonSelector);
            nextPageButton.Click();
        } while (true);
    }
}

0 个答案:

没有答案