F
F
FairyFox57002019-04-13 16:51:31
Parsing
FairyFox5700, 2019-04-13 16:51:31

How to use Task to parse a directory in C#?

I have a product catalog online.
I want to write data to a database, but without threads it takes over 2 hours.
There are car brands on the site, then if you go to the page by clicking on the name of the brand, there will be several models, and in the models, respectively, submodels.
How to use Task in C# to speed up this process?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ascar, 2019-04-14
@FairyFox5700

static void Main(string[] args)
        {       
            ExampleMyltiTask();
            Console.ReadLine();
        }

       static HttpClient http = new HttpClient();

        static async Task ExampleMyltiTask()
        {

            int pages = 100;
            while (pages-- > 0)
            {
                var tasks = new List<Task>();

                int count = 10;

                while (count-- > 0)
                {
                    var task = Task.Run(async () =>
                    {
                        try
                        {
                            var item = await http.GetStringAsync("item url");
                            //парсинг item
                            // асинхронная запись в бд, например await context.SaveChangesAsync(); 
                            Console.Write("записано" + Environment.NewLine);
                        }
                        catch
                        {
                            //
                            Console.Write("ошибка" + Environment.NewLine);
                        }
                    });

                    tasks.Add(task);
                }

               await Task.WhenAll(tasks);

                //переход на след страницу             
                Console.Write("следующая страница" + Environment.NewLine);
            }
     }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question