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> /// Display the result /// </summary> public void DisplayOutputs(bool Exact, Result result) { if (!((result.BaseResistors.R1 == 0) && (result.BaseResistors.R2 == 0))) { if (CheckboxExact.Checked == true) { // Affichage des valeurs exactes textbox_outR1.Text = result.BaseResistors.R1.ToString() + "Ω"; textbox_outR2.Text = result.BaseResistors.R2.ToString() + "Ω"; textbox_outR.Text = result.Resistor.ToString(); textbox_outError.Text = $"{result.Error}%"; } else { // Affichage des valeurs arrondies textbox_outR1.Text = Tools2.DecimalToEngineer(result.BaseResistors.R1); textbox_outR2.Text = Tools2.DecimalToEngineer(result.BaseResistors.R2); textbox_outR.Text = Tools2.DecimalToEngineer(Math.Round(result.Resistor, 3)); textbox_outError.Text = $"{Math.Round(result.Error, 3)}%"; } labelSerieParallel.Text = result.Parallel ? "||" : "+"; } }
/// <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(); }
/// <summary> /// Display text containing all results /// </summary> public void DisplayList(BackgroundWorker b) { msg = string.Empty; // Use multiple strings to increase speed List <string> msgs = new List <string>(); // Add header line msgs.Add(string.Empty); // Progress int progress; // Line counter int count = 0; // Number of lines int max_i = Results.Count; // Result to display Result result; // Fixed length of the longest double value to be displayed (to have a nice formatted text) int maxlength = 0; // Is exact mode enabled if (Exact) { WriteLog("Exact mode ON", Logger.Log_level.Debug); maxlength = maxRes.ToString().Length; msgs[0] += $"{"R1".PadRight(maxlength)} {" "} {"R2".PadRight(maxlength)} {"Req".PadRight(16)} {"Error [%]"}{Environment.NewLine}"; } else { WriteLog("Exact mode OFF", Logger.Log_level.Debug); msgs[0] += $"{"R1".PadRight(6)} {" "} {"R2".PadRight(6)} {"Req".PadRight(9)} {"Error [%]"}{Environment.NewLine}"; } // Loop every results for (int i = 0; i < max_i; i++) { // If cancel requested if (b.CancellationPending) { WriteLog("Display List skipped", Logger.Log_level.Info); break; } // Retrieve result result = Results[i]; // If exact mode enabled if (Exact) { msgs[count] += $"{result.BaseResistors.R1.ToString().PadRight(maxlength)} {(result.Parallel ? "||" : " +")} {result.BaseResistors.R2.ToString().PadRight(maxlength)} = {result.Resistor.ToString().PadRight(16)} {((result.Error >= 0) ? (" ") : (""))}{result.Error}{Environment.NewLine}"; } else { msgs[count] += $"{Tools2.DecimalToEngineer(result.BaseResistors.R1).PadRight(6)} {(result.Parallel ? "||" : " +")} {Tools2.DecimalToEngineer(result.BaseResistors.R2).PadRight(6)} = {Tools2.DecimalToEngineer(Math.Round(result.Resistor, 3)).PadRight(9)} {((result.Error >= 0) ? (" ") : (""))}{Math.Round(result.Error, 3)}{Environment.NewLine}"; } // Switch to next slot every 5k lines if (i % 5000 == 0) { // Update to preview Window count++; // Add next slot msgs.Add(string.Empty); // Report progress progress = ((i * 100) / max_i); b.ReportProgress(progress); } } // Text generated, appending label_status.Text = "Appending text..."; for (int i = 0; i < msgs.Count; i++) { msg += msgs[i]; if (i % 100 == 0) { progress = (i * 100) / msgs.Count; b.ReportProgress(progress); } } label_status.Text = "Loading window"; b.ReportProgress(100); }