R
R
rundll322022-03-27 05:25:21
Asynchronous programming
rundll32, 2022-03-27 05:25:21

ProgressBar with asynchronous tasks, how to do it right?

There is a progress bar on the form, which is filled in several steps as resources are loaded, something like this:

void LoadResources() {
   LockUI(); // выключаем кнопки и т.д.
   LoadImages();
   progressBar1.Value = 25;
   LoadMusic();
   progressBar1.Value = 50;
   LoadFonts();
   progressBar1.Value = 75;
   LoadIcons();
   progressBar1.Value = 100;
   UnlockUI(); // включаем кнопки и т.д.
}


At the same time, the form with this progressbar should not hang during execution. The most obvious solution seems to be Task.Run, which will have this method, but all I know is that you have to turn off the Control.CheckForIllegalCrossTreadCall flag, otherwise there will be an exception. But as I understand it, the approach itself is not very correct, and this flag is also not just there, and turning it off is a crutch. How is it done right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question