public async void execute() { if (this.taskLimit <= 0) { return; } List <Task> taskList = new List <Task>(); while (true) { try { taskList.Add(Task.Run(() => { (new YandexEther(this.etherUrl)).parse(); })); } catch { } Console.WriteLine("=====>Добавлена задача!Всего {0} задач", taskList.Count); Thread.Sleep(YandexUtils.rndSleep()); if (taskList.Count >= this.taskLimit) { Task.WaitAny(taskList.ToArray()); try { taskList .Where(t => t.IsCompleted) .ToList() .ForEach(t => { taskList.Remove(t); }); } catch { } } } }
public async void execute() { Console.WriteLine("Запуск процесса зборщика слова"); List <Task> taskList = new List <Task>(); long count = this.fileDataCount(); while (true) { kwMutext.WaitOne(); var globalOffset = this.offset; if (loadFromFile) { globalOffset = !settings.KeyExists("file_offset", SETTINGS_SECTION) ? long.Parse(settings.Write("file_offset", "" + file_offset, SETTINGS_SECTION)) : long.Parse(settings.Read("file_offset", SETTINGS_SECTION)); } else { globalOffset = !settings.KeyExists("offset", SETTINGS_SECTION) ? long.Parse(settings.Write("offset", "" + offset, SETTINGS_SECTION)) : long.Parse(settings.Read("offset", SETTINGS_SECTION)); } this.offset = Math.Max(globalOffset, this.offset); this.limit = (STEP - Math.Min(taskList.Count, STEP)); this.offset += (STEP - Math.Min(taskList.Count, STEP)); settings.Write(this.loadFromFile ? "file_offset" : "offset", "" + this.offset, SETTINGS_SECTION); List <DBKeyword> list = loadFromFile ? loadFile(offset, limit) : this.listKeywords(offset, limit); if (list == null) { kwMutext.ReleaseMutex(); break; } kwMutext.ReleaseMutex(); list.ForEach(kw => { taskList.Add(Task.Run(() => { this.takeKW(kw); })); Thread.Sleep(YandexUtils.rndSleep()); }); Console.WriteLine("Задач в таске:" + taskList.Count()); Task.WaitAny(taskList.ToArray()); try { taskList .Where(t => t.IsCompleted) .ToList() .ForEach(t => { taskList.Remove(t); }); } catch { } if (loadFromFile && offset >= count) { settings.Write("loadFromFile", "false", SETTINGS_SECTION); offset = long.Parse(settings.Read("offset", SETTINGS_SECTION)); loadFromFile = false; Console.WriteLine("Переключаемся на выборку из БД"); Thread.Sleep(YandexUtils.rndSleep()); } Thread.Sleep(YandexUtils.rndSleep()); Console.WriteLine("offset=>" + offset + " count=>" + count); } }