E
E
EGGOFFOX2021-09-18 17:04:25
C++ / C#
EGGOFFOX, 2021-09-18 17:04:25

Why is progressBar slow?

The result of exponentiation appears faster than filling the ProgressBar
Code:

private long Pow(long number, int power) {
            var result = 1L;
            progressBar1.Maximum = power;
            if (power == 0)
            {
                progressBar1.Maximum = 1;
                progressBar1.Value = 1;
            }
            else
            {
                progressBar1.Value = 0;
                while (power > 0)
                {
                    if ((power & 1) == 0)
                    {
                        number *= number;
                        power >>= 1;
                        progressBar1.Value = progressBar1.Value + power;
                    }
                    else
                    {
                        result *= number;
                        --power;
                        ++progressBar1.Value;
                    }
                }
            }
            return result;
        }
          
        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            label3.Text = Pow((long)numericUpDown1.Value, (int)numericUpDown2.Value).ToString();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            label3.Text = Pow((long)numericUpDown1.Value, (int)numericUpDown2.Value).ToString();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
HemulGM, 2021-09-18
@EGGOFFOX

Because the ProgressBar component fills smoothly with the OS style. Those. cannot exceed some standard filling rate. Even if you set the value to 0, and then immediately 100, then it will be filled for almost a second. If you want to show the "real" fill rate - draw your own. For example, just place a couple of rectangles on the form. The main one is the frame of the progress bar and the Inner one, which will serve as a fill. Set the size of the inner rectangle based on the percentage of filling of the "progressbar".
And a tip: the progressbar should always have a maximum size of 100 values. 100%. Do not set absolute values ​​for the limit and minimum. Always work with percentages.

D
Developer, 2021-09-18
@samodum

Because there is no multithreading. And wrong work with UI. UI is not updated instantly, learn how it works.

N
none7, 2021-09-18
@none7

WinForms, in principle, is not a fast thing. Try to reduce the number of progressbar value settings to the size of this control in pixels and no more than 10 changes per second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question