private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { entity.Dispose(); } GC.Collect(); disposedValue = true; } }
public static void StartDownloadVoabulary(object obj) { var VocabularyCom = AppDomain.CurrentDomain.BaseDirectory + "\\VocabularyCom\\"; Directory.CreateDirectory(VocabularyCom); do { var entity = new EnglishWordsEntities(); List <Task> taskList = new List <Task>(); var rows = entity.Roots.Where(x => x.VocabularyCom == null //&& x.DictionaryCom != null //&& x.DictionaryCom.Value ) .Take(10).ToList(); if (rows.Count > 0) { foreach (var row in rows) { var task = Task.Factory.StartNew(() => { try { var resulT = GetDataVocabulary(row, VocabularyCom + row.ID + ".txt", "https://www.vocabulary.com/dictionary/" + row.Word); resulT.Wait(); var result = resulT.Result; var entity2 = new EnglishWordsEntities(); result.CreateDate = DateTime.Now; if (result.VocabularyComStatusCode == 403) { result.VocabularyCom = null; } entity2.Roots.AddOrUpdate(result); entity2.SaveChanges(); if (result.VocabularyComStatusCode == 200) { if (result.VocabularyCom != null && result.VocabularyCom.Value) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"{DateTime.Now} Vocabulary Found ... " + row.Word); } else { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"{DateTime.Now} Vocabulary Not Found ... " + row.Word); } } else if (result.VocabularyComStatusCode == 403) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{DateTime.Now} Vocabulary Forrbiden ... " + row.Word); } else { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine($"{DateTime.Now} Vocabulary {result.VocabularyComStatusCode ?? 0}... " + row.Word); } //Console.WriteLine("Saved " + result.Word); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); } }); taskList.Add(task); } Task.WaitAll(taskList.ToArray()); //Task.Delay(1000).Wait(); taskList.Clear(); entity.Dispose(); GC.Collect(); } else { break; } } while (true); }