While I understand with async \ await I decided to make another backup option if nothing happens with async \ await. While the program will think, let the ProgressBar fill.

private void button1_Click(object sender, EventArgs e) { int T = Convert.ToInt32(textBox1.Text); int U = Convert.ToInt32(textBox2.Text); double shag = Convert.ToDouble(textBox3.Text); int frc = Convert.ToInt32(textBox4.Text); int frc1 = Convert.ToInt32(textBox5.Text); int frequency, counter; List<Complex> values = new List<Complex>(); progressBar1.Value = 0; var timer = new System.Windows.Forms.Timer(); timer.Interval = 100; timer.Tick += (s, a) => { progressBar1.Value += 5; if (progressBar1.Value == progressBar1.Maximum) { timer.Stop(); button1.Enabled = true; } }; timer.Start(); for (frequency = frc, counter = 1; frequency <= frc1; frequency++, counter++) { values.Add(reverstrans(U, frequency, T, frc, frc1,shag)); } values.ForEach(x1 => dataGridView1.Rows.Add(x1.Real, x1.Imaginary)); drawreverse(U,T, shag, frc, frc1); drawgraph(U, T, frc, frc1, frequency, shag); } 

As soon as the calculations end then he is immediately green.

But it was planned to fill it in as the calculation process, thereby improving the interface of the program.

After the code I changed

  private void button1_Click(object sender, EventArgs e) { int T = Convert.ToInt32(textBox1.Text); int U = Convert.ToInt32(textBox2.Text); double shag = Convert.ToDouble(textBox3.Text); int frc = Convert.ToInt32(textBox4.Text); int frc1 = Convert.ToInt32(textBox5.Text); int frequency, counter; List<Complex> values = new List<Complex>(); progressBar1.Maximum = 100; for (frequency = frc, counter = 1; frequency <= frc1; frequency++, counter++) { values.Add(reverstrans(U, frequency, T, frc, frc1,shag)); progressBar1.Value += frequency ; } values.ForEach(x1 => dataGridView1.Rows.Add(x1.Real, x1.Imaginary)); drawgraph(U, T, frc, frc1, frequency, shag); drawreverse(U, T, shag, frc, frc1); } 

but an exception popped up and at the same time, from where he adds to my ProgressBar1.Maximum, the number 28.

enter image description here

enter image description here

I tried changing the settings step and maximum but the exception crashed and those cases and returned to the original parameters

I looked at similar questions and my code coincides partly with what they have. But knocks an exception.

  • one
    Well, everything is logical, you have a progressbar with a max valid value of 50, and you try to assign a value greater than this to it (Value + = frequency), this is written directly to you in error. - Sergiy
  • I have already set the value to 5000 and still he writes that 5001 is the value But after all, the step must somehow be fixed, the moving strip. What needs to be done to get rid of this problem? I'm not asking for the code, just where to look in which direction? - beginner

1 answer 1

The issue was resolved by me

After the green bar is filled, the result comes out and the bar moves during the calculations. And there are no exceptions.

  progressBar1.Maximum = 5000; progressBar1.Step = 1; for (double fr = frc; fr <= frc1; fr += shag) { double resulting = (1 / (2 * Math.PI)) * integration.Calculate(angularFrequency => GetSpectralDensityOfAmplitude(GetSpectralDensity(U, angularFrequency, T)) * Math.Cos(angularFrequency * t), 0, frc1); list1.Add(fr, resulting); progressBar1.PerformStep(); }