private void OnSearch(object sender, EventArgs e) { _totalPages = 0; _parsedPages = 0; _form.PageCount = 0; _form.ResultCount = 0; _form.CanSearchNext = false; object state = _form.BeginSearching(); try { Layout layout = _form.GetLayout(); if (new IsEmptyString(layout.SearchText)) { return; } WebPageInfo pageInfo = new AvitoPage(_settings, new AvitoPageRequest(_settings, layout.SearchText)).Info(); if (pageInfo.PageCount == 0) { return; } _form.PageCount = _totalPages = pageInfo.PageCount; for (int i = 1; i <= layout.PagesPerSet; i++) { _parsedPages++; if (_parsedPages > _totalPages) { return; } List <WebSearchResult> newResults = SearchOnPage(i, layout); for (int j = 0; j < newResults.Count; j++) { _form.ResultCount++; _form.AddResultEntry(newResults[j]); } } } finally { _form.EndSearching(state); _form.CanSearchNext = _parsedPages < _totalPages; SortBy(); } }