/// <summary> /// paralell parse chapters info /// </summary> /// <param name="threadsNumber">how many threads is running at the same time</param> /// <param name="match">add ChapterInfo to list if ShortChapterInfo match</param> protected void ParseChaptersInfo(Predicate <ShortChapterInfo> match, int threadsNumber) { IChapterJsonParser jsonParser = new ChapterJsonParser(); Thread[] threads = new Thread[threadsNumber]; int threadsIndex = 0; foreach (var chapter in MangaInfo.ShortChaptersInfo) { // check if it s match the request if (match(chapter)) { // we use post increament so we just compare this values if (threadsIndex == threadsNumber) { threadsIndex = 0; // start threads foreach (var thread in threads) { thread.Start(); } //// wait for thread's work complete //foreach (var thread in threads) //{ // thread.Join(); //} } // parse json func ThreadStart parseFunc = () => { // wait for calling thread wait for end of the parsing // parse data from site var chapterLocal = chapter; int id = Convert.ToInt32(chapterLocal.Id); IChapterInfo info = jsonParser.GetChapterInfo(id); ChaptersInfo.Add(info); }; threads[threadsIndex++] = new Thread(parseFunc); } } // if something left in threads array and yeah there only matched chapters if (threadsIndex > 0) { // threadsIndex is size of the array with useful parseFuncs // start threads for (int i = 0; i < threadsIndex; i++) { threads[i].Start(); } // wait for thread's work complete for (int i = 0; i < threadsIndex; i++) { threads[i].Join(); } } }
/// <summary> /// parse chapters info /// </summary> /// <param name="match">predicate for chapters</param> public List <IChapterInfo> ParseChaptersInfo(Predicate <ShortChapterInfo> match) { IChapterJsonParser jsonParser = new ChapterJsonParser(); List <IChapterInfo> chaptersInfo = new List <IChapterInfo>(); foreach (var chapter in MangaInfo.ShortChaptersInfo) { // check if it s match the user's request if (match(chapter)) { int id = Convert.ToInt32(chapter.Id); IChapterInfo info = jsonParser.GetChapterInfo(id); chaptersInfo.Add(info); } } return(chaptersInfo); }