1

我完全迷失在 .NET 中的新异步方法中。我的问题是我有一个方法

private static List<RouteDTO> ParseRoutesHTML(string result, Cookie cookie)
        {
            List<RouteDTO> routes = new List<RouteDTO>();
            HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
            htmlDocument.LoadHtml(result);
            int contNumber = 1;
            while (true)
            {

                HtmlNode divNode = htmlDocument.GetElementbyId("cont"+contNumber);
                if (divNode != null)
                {
                    HtmlNode table = divNode.SelectSingleNode("table");
                    if (table != null)
                    {
                        string fullRoute = "";
                        HtmlNodeCollection dataRows = table.SelectNodes("tr[@align]");
                        RouteDTO currentRoutes = new RouteDTO();
                        foreach (var dataRow in dataRows)
                        {
                            currentRoutes.routes.Add(ParseRouteDataRow(dataRow));
                            //fullRoute+=ParseDataRow(dataRow)+" then ";
                        }
                        HtmlNodeCollection lastRow = table.SelectNodes("tr").Last().SelectSingleNode("td/div").SelectNodes("a");
                        HtmlNode mapLink = lastRow[1];

                        ParseMap(currentRoutes, mapLink);


                        HtmlNode priceLink = lastRow[2];
                        string priceHref = priceLink.Attributes["href"].Value;

                        ParcePrice(currentRoutes, priceHref, cookie);

                        routes.Add(currentRoutes);
                    }
                    contNumber++;
                }
                else
                {
                    break;
                }
            }
            return routes;
        }

private static void ParcePrice(RouteDTO currentRoutes, string priceHref, Cookie cookie)
        {
            var cookieContainer = new CookieContainer();
            var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
            var httpClient = new HttpClient(handler);
            cookieContainer.Add(cookie);

            var priceMessage = httpClient.GetByteArrayAsync("http://razpisanie.bdz.bg" + priceHref).Result;
            var priceResponse = Encoding.UTF8.GetString(priceMessage, 0, priceMessage.Length - 1);
            var priceInfo = ParsePriceResponse(priceResponse);
            currentRoutes.priceInfo = priceInfo;
        }

        private static void ParseMap(RouteDTO currentRoutes, HtmlNode mapLink)
        {
            var httpClient = new HttpClient();
            string mapHref = mapLink.Attributes["href"].Value;
            var mapMessage = httpClient.GetByteArrayAsync("http://razpisanie.bdz.bg" + mapHref).Result;
            var mapResponse = Encoding.UTF8.GetString(mapMessage, 0, mapMessage.Length - 1);
            string mapString = ParseMapResponse(mapResponse);
            currentRoutes.imageBase64 = mapString;
        }

请不要介意糟糕的 HTML 解析(网站本身很糟糕),但请看一下 ParseMap 和 ParsePrice 方法。它们发出 Web 请求,因此需要异步。问题是,如果我让它们异步,我担心它们在被调用时不会完成return routes(这实际上是我使 void 异步时发生的事情,但我发现这是一个很大的禁忌)。如何return routes在调用之前等待所有异步方法完成?

4

1 回答 1

1

从您的每个解析方法中返回任务,将它们放在一个列表或数组中,然后使用 Task.WhenAll 创建一个任务,该任务将在所有任务完成后完成并从您的操作中返回。

于 2013-09-10T13:59:52.663 回答