private void WriteDateTimeToSql(LoadCpu loadCpu, Model_dateTime modelDTBase, int newIndexDb) { using (SqlContext db = new SqlContext()) { statusConnSql = true; try { // check id for duplicates Model_dateTime myEntity = db.model_dateTime.FirstOrDefault(p => p.Id_DateTime == modelDTBase.Id_DateTime); if (myEntity == null) { // add to BD SQL -------------------------WRITE-----<-----<-----< db.model_dateTime.Add(modelDTBase); db.SaveChanges(); } loadCpu.WritePositionLimitsCpu(newIndexDb); // write PositionRead } catch (Exception ex) { Thread.Sleep(Properties.Settings.Default.timerException); try { // add to BD SQL -------------------------WRITE-----<-----<-----< db.model_dateTime.Add(modelDTBase); db.SaveChanges(); statusConnSql = true; } catch (Exception ex2) { logger.Error(ex2.Message, " --- WriteDateTimeToSql(Model_dateTime modelDTBase, int newIndexDb) --> no load MAX Id"); statusConnSql = false; } logger.Error(ex.Message, " --- WriteDateTimeToSql(Model_dateTime modelDTBase, int newIndexDb) --> no load MAX Id"); Thread.Sleep(Properties.Settings.Default.timerException); statusConnSql = false; } } }
private void readSettingBtn_Click(object sender, EventArgs e) { loadCpu.WritePositionLimitsCpu(Convert.ToInt32(readSetLabel.Text)); // write PositionWrite (+1) }
public WorkCpu() { LoadCpu loadCpu = new LoadCpu(); while (true) { // STOP if (Form1.stopThreadAll) { return; } workCpuStatus = true; // Makes the status, for a long time there was no connection with the CPU NoConnectionTimeSec(loadCpu); // select limits--------------- LimitsCpu limitsCpu = loadCpu.ReadLimits(); if (limitsCpu == null) { continue; // error connect } if ((limitsCpu.PositionRead < limitsCpu.PositionMin) || (limitsCpu.PositionRead > limitsCpu.PositionMax)) { loadCpu.WritePositionLimitsCpu(limitsCpu.PositionMin); // write PositionRead continue; } //------------------------------ writeIndex = limitsCpu.PositionWrite; readIndex = limitsCpu.PositionRead; // new PositionRead + 1 int newIndexDb = LocationIndexDb(loadCpu, limitsCpu); // if there isn't data if (newIndexDb < limitsCpu.PositionMin) { FindMaxIdFromSql(); continue; } else { // loading data all CPU-------- Model_dateTime modelDTBase = loadCpu.ReadDatetime(newIndexDb); if (modelDTBase == null) { continue; // error connect } if (modelDTBase.Id_DateTime == -1) { loadCpu.WritePositionLimitsCpu(newIndexDb); // write PositionWrite (+1) continue; } //------------------------------ idMaxSql = modelDTBase.Id_DateTime; // save ID to static // ok <<< Write id and DateTime to SQL WriteDateTimeToSql(loadCpu, modelDTBase, newIndexDb); } if (Form1.stopThreadAll) { return; // STOP } Thread.Sleep(Properties.Settings.Default.timerWorkCycle); } }