C #: displaying a modal dialogue of progress over a long process

My software requires storing the directory and registry structure for some specific locations. This usually takes a long time. Let's say I have a method called SaveState()that does this.

I want to wrap this in another method SaveStateWithProgress(), so when I call it, a modal dialog will appear that shows a simple indication of progress and has a button to cancel the operation. As I see it, I may need to use two threads, although in VB6 or Java that I used with the command Thread.Yield()I’m not sure if this is the best practice, and even if there is something like that in C #, What is the best way to do this ?

+3
source share
4 answers

Here is a site that I think will satisfy what you need.

It has an example of using the progress bar and background (using BackgroundWorker.RunWorkerAsync ()).

+4
source

The best way in C # is to use BackgroundWorker and perform an intensive operation inside this working background.

Here is a tutorial that contains instructions on how to cancel an operation halfway.

+6
source

# Thread.Sleep(0) Thread.Sleep(1), , Java Thread.Yield().

With that said, you will definitely need to make yours SaveState()in a separate thread. Have a variable in your function SaveState()that updates as it progresses SaveState(). Put a timer in your modal progress form and ask the timer to check this variable is updated SaveState(). Update your progress bar

-1
source
ProgressBar p = new ProgressBar();
p.Location = new Point(10, 10);
p.Size = new Size(100, 30);
p.MarqueeAnimationSpeed = 30;
p.Style = ProgressBarStyle.Marquee;
-2
source

Source: https://habr.com/ru/post/1753549/


All Articles