private void timerSave_Tick(object sender, EventArgs e) { if (!SavingDataBase) { SavingDataBase = true; MoleculeGraph molecule = null; Molecule saved = null; try { if (ResultQueue.Count > 0) { lock (queueObjectLock) { molecule = ResultQueue.Dequeue(); ShowQueueDataSource(); } if (molecule != null && _moleculeService.GetByIdStructure(molecule.Nomenclature, molecule.IdStructure) == null) { if (!string.IsNullOrEmpty(molecule.IdStructure)) { saved = _moleculeService.Create(molecule); } var emptyMolecule = _moleculeService.GetByIdStructure(molecule.Nomenclature, null); if (emptyMolecule != null && _moleculeService.GetNotEmptyMoleculeCount(molecule.Nomenclature) > 0) { _moleculeService.Delete(emptyMolecule); } } if (saved != null) { SearchCount++; DatabaseCount++; } } DatabaseCount = _moleculeService.GetMoleculeCount(); } catch (Exception) { lock (queueObjectLock) { if (molecule != null && saved == null) { ResultQueue.Enqueue(molecule); } ShowQueueDataSource(); } } finally { SavingDataBase = false; } } }
public static void Main(string[] args) { Console.WriteLine("Importar moléculas de arquivos JSON?"); Console.Write("(S)Sim (N)Não: "); var key = Console.ReadKey(); if (key.KeyChar.ToString().ToUpper() == "S") { try { GetServices(); var fg = new FormulaGenerator(); var JsonLoader = new JsonDataSet(); new Thread(() => { JsonLoader.ConsumeJsonFiles(); }).Start(); var counter = 1; while (JsonLoader.FormulasQueue.Count > 0 || !JsonLoader.Finished) { if (JsonLoader.FormulasQueue.Count > 0) { var molecule = JsonLoader.GetMolecule(); var formula = fg.GetFormulaFromMolecule(molecule); if (moleculeService.GetByNomenclature(formula).Count == 0 && molecule.Sum(x => x.Value) >= 2 && molecule.Sum(x => x.Value) <= 50) { Console.WriteLine(string.Format("{0} - Salvando molécula {1}", counter++, formula)); moleculeService.Create(new Molecule() { AtomsCount = molecule.Sum(x => x.Value), DiferentAtomsCount = molecule.Count, SimpleAtoms = string.Empty, SimpleLinks = string.Empty, Nomenclature = formula, IdStructure = string.Empty, Energy = 0, FromDataSet = true }); } } else { Thread.Sleep(1000); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }