public void ButtonConfirm_Click(object sender, EventArgs e) { if (Running) { WriteLog("Process already running", Logger.Log_level.Info); return; } shownResult = 0; if (double.TryParse(TextBoxResistor.Text, out double Resistor)) { if (Resistor > 0) { GetResistors(Resistor); } else { WriteLog("Incorrect value, positive numbers only : " + TextBoxResistor.Text, Logger.Log_level.Error); MessageBox.Show("Value incorect : Positive numbers only\nFollow this example :\n24.56k", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { Resistor = Tools2.EngineerToDecimal(TextBoxResistor.Text); if (Resistor > 0) { GetResistors(Resistor); } else { WriteLog("Incorrect value, invalid characters : " + TextBoxResistor.Text, Logger.Log_level.Error); MessageBox.Show("Value incorect : Positive numbers only\nFollow this example :\n24.56k", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Run the worker to get resistors /// </summary> public void GetResistors(double Resistor) { // Retrieve the current serie Serie = Series.GetSerie(CurrentSerie); // Clear output text ClearOutput(); // Get settings // Min resistor minRes = Tools2.EngineerToDecimal(textbox_RL1.Text); if (minRes == 0) { WriteLog("Incorrect minRes", Logger.Log_level.Error); return; } // Max resistor maxRes = Tools2.EngineerToDecimal(textbox_RL2.Text); if (maxRes == 0) { WriteLog("Incorrect maxRes", Logger.Log_level.Error); return; } // If min is > than max if (minRes > maxRes) { WriteLog("Min res is greater than Max res", Logger.Log_level.Warn); MessageBox.Show("Minimum resistor must be greater or equal than maximum resistor", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // If error is not decimal if (!double.TryParse(textbox_Error.Text, out minError)) { WriteLog("Min error incorrect", Logger.Log_level.Error); MessageBox.Show("Invalid minimum error value\n" + minError, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // If buffer is not decimal if (!int.TryParse(textbox_Buffer.Text, out buffersize)) { WriteLog("Buffer incorrect", Logger.Log_level.Error); MessageBox.Show("Invalid buffer size value\n" + buffersize, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Set maxresults to buffer size maxResults = buffersize; // Write to log WriteLog(string.Format("Processing with config : R={0} | Rmin={1} | Rmax={2} | ErrMin={3} | MaxResults={4}", Resistor, minRes, maxRes, minError, maxResults), Logger.Log_level.Debug); // Set desired resistor desiredResistor = Resistor; // If reistor less or equal to 0 if (Resistor <= 0) { WriteLog("Invalid resistor value", Logger.Log_level.Warn); MessageBox.Show("Invalid resistor value\n" + Resistor, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Set cursor and flags Cursor = Cursors.WaitCursor; Running = true; state = 0; // Run the Backgroundwoker (Bw_DoWork) WriteLog("Running background worker ", Logger.Log_level.Debug); bw = new BackgroundWorker(); bw.DoWork += Bw_DoWork; bw.RunWorkerCompleted += Bw_RunWorkerCompleted; bw.WorkerSupportsCancellation = true; bw.WorkerReportsProgress = true; bw.ProgressChanged += Bw_ProgressChanged; bw.RunWorkerAsync(); }