In my large application, the time has come that you need to do long operations such that they are executed in a pending mode and do not interfere with the main flow. I looked at simple examples like everything is clear; if the method is more complicated, then porridge is obtained. Here is the code for setting the problem. It commented on the lines where I tried to start the task in different ways, here is my algorithm of understanding, tell me where I am wrong.
- Create a create method in the form of an asynchronous Getpage task.
- I create the main asynchronous method, in which my asynchronous task from p. 1 is also present. I put the await operator in front of an asynchronous task (method), thus I let the compiler know that this task inside my method will be executed asynchronously.
- I try to run the task asynchronously in the Main method in several ways.
Logically, I understand correctly? What do you need for an asynchronous task to create an external TASK? How it is all organized.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using HtmlAgilityPack; using System.Web; namespace probaparser { class Program { static void Main(string[] args) { Info i = new Info(); string urrrla = "https://www.rabota66.ru/resume/rbranch1?page=0"; Console.WriteLine(string.Join(" ", GetResumme(urrrla))); Console.ReadKey(); } // Создаю метод ввиде задачи, которая будет выполняться асинхролнно. public static Task<string> Getpage(string url) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.AllowAutoRedirect = false; httpWebRequest.Referer = "http://google.com"; // Реферер. Тут можно указать любой URL using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) { using (var stream = httpWebResponse.GetResponseStream()) { using (var reader = new StreamReader(stream, Encoding.GetEncoding(httpWebResponse.CharacterSet))) { // читаем ассинхронно построчно return reader.ReadLineAsync(); } } } } // метод который выполнятся должен асинхронно. // await поставил перд методом, тип которого задача,которая возвращает public static async Task<List<Info>> GetResumme(string ReadyString) { HtmlAgilityPack.HtmlDocument document = new HtmlDocument(); document.LoadHtml(await Getpage(ReadyString)); var list = new List<Info>(); // получаем резюме var items = document.DocumentNode.SelectNodes("//ul[@class='visitor-resume-list']/li"); foreach (var item in items) { var name = item.SelectSingleNode(".//h2[@data-ga='resumeListItem']").InnerText; var data = item.SelectSingleNode(".//div[@class='title-']/following-sibling::div") .SelectNodes(".//p"); var salary = item.SelectSingleNode(".//b[@class='salary-']").InnerText; var Prof_info = item.SelectNodes(".//dl[@class='prof-path-']/p"); string kartinka; try { kartinka = item.SelectSingleNode(".//a[@class='vrloa-img img_zoom']").Attributes["href"].Value; } catch (Exception e) { kartinka = "Нет изображения"; } string Data = string.Join("\n", data.Select(x => string.Format(" {0}", x.InnerText.Trim()))); if (Prof_info != null) { string prooof = string.Join("\n", Prof_info.Select(x => string.Format(" >{0}", x.InnerText.Trim()))); list.Add(new Info() { VacansName = name, Name = Data, Salary = salary, Expiriens = prooof, Foto = kartinka }); } } return list; } } } Here is the class for organizing the collection, it is auxiliary.
public class Info { public string VacansName { get; set; } public string Name { get; set; } // public string PojelaniyKRabote { get; set; } public string Expiriens { get; set; } public string Salary { get; set; } public static string VakansyRubrick { get; set; } public string Foto { get; set; } }
static Task Main(string[] args)andstring.Join(" ", await GetResumme(urrrla))- Grundy