コード例 #1
0
ファイル: App.cs プロジェクト: INIage/net-courses-2019
        public async Task Run(string url, int depth)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                UrlService urlService = new UrlService(unitOfWork);
                await Task.Run(() =>
                {
                    Thread.Sleep(500);

                    if (depth > 0)
                    {
                        int iteration   = urlService.GetCurrentIteration();
                        string filename = $"Pages\\{url.GetHashCode()}.html"; //Pages\\
                        this.downloadService.DownloadHtml(url, filename);
                        List <string> linksToAdd = this.parsingService.GetLinksFromHtml(filename, url);
                        if (linksToAdd != null)
                        {
                            //lock (locker)
                            //{
                            mutexObj.WaitOne();
                            urlService.AddParsedLinksToDB(linksToAdd, iteration);
                            mutexObj.ReleaseMutex();
                            //}
                        }

                        if (depth > 1)
                        {
                            var linksFromPreviousIteration = urlService.GetAllLinksByIteration(iteration).ToList();
                            if (linksFromPreviousIteration.Count > 0)
                            {
                                Parallel.ForEach <string>(linksFromPreviousIteration, (link) =>
                                {
                                    Task t = Run(link, depth - 1);
                                    t.Wait();
                                });
                            }
                        }
                    }
                });
            }
        }