It may not seem like progress in animation

OK, so I have a problem with this.

public Class A{ public A(){ progressBar.Style = ProgressBarStyle.Marquee; progressBar.MarqueeAnimationSpeed = 0; } public void DoSomething(){ if(checkpasses){ progressBar.MarqueeAnimationSpeed = 100; //Do something here... progressBar.MarqueeAnimationSpeed = 0; } else //Do nothing... } } 

The problem is that my progress will not begin to move at all. At first I decided that I myself would not create a new stream (which I find wired), so I tried to create a stream, but still the same result. Nothing happened. Did I forget something?

+6
source share
4 answers

The "do something here" code will block the user interface thread, so you won’t see the update of the progress bar until the DoSomething method completes. At this time, you set the animation speed to 0.

Try entering the "do something here" code in a separate thread. When this thread completes, set the animation speed back to 0.

Something like that:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBar1.MarqueeAnimationSpeed = 0; progressBar1.Style = ProgressBarStyle.Blocks; progressBar1.Value = progressBar1.Minimum; } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { DoSomething(); } private void button1_Click(object sender, EventArgs e) { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.MarqueeAnimationSpeed = 100; backgroundWorker1.RunWorkerAsync(); } private void DoSomething() { Thread.Sleep(2000); } } 
+6
source

Call

 Application.EnableVisualStyles(); 

at the very beginning of your application.

+15
source

I'm not sure if this is the best solution, but I have it like this:

  //this is the action item (button click) private void importSFNFReportButton_Click(object sender, EventArgs e) { //I run backgroundWorker6Progress.RunWorkerAsync(); //this is how I start the progress bar 'movement' bgwImportSF.RunWorkerAsync(); //this is another task that is lauchned after the progress bar is initiated } 

This is the actual background worker.

  private void backgroundWorker6Progress_DoWork(object sender, DoWorkEventArgs e) { bool cont = true; while (cont) { PauseForMilliSeconds(100); updateProgressbar1(false); if (noTasksExistCheck()) { updateProgressbar1(true); cont = false; } } } 

this is a delegate - I call it to automatically increase the progress bar indicator

  delegate void updateProgressBarStatus(bool done); private void updateProgressbar1(bool done) { if (progressBar1.InvokeRequired) { updateProgressBarStatus del = new updateProgressBarStatus(updateProgressbar1); progressBar1.Invoke(del, new object[] { done }); } else { if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum; } progressBar1.PerformStep(); if (done == true) { progressBar1.Value = progressBar1.Minimum; } } } 

I control it through a function that should check the global variable

 noTasksExistCheck() 

This is a pause timer

  public static DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor) { System.DateTime ThisMoment = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor); System.DateTime AfterWards = ThisMoment.Add(duration); while (AfterWards >= ThisMoment) { System.Windows.Forms.Application.DoEvents(); ThisMoment = System.DateTime.Now; } return System.DateTime.Now; } 
0
source

To supplement some more, the solution proposed by Dave will work only if Konstantin suggested the code. Otherwise, you should consider manually increasing the value of progressbar.value in a loop with the following code in DoWork: BeginInvoke (new MethodInvoker (() => progressBarSave.Value + = progressBarSave.Step));

0
source

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


All Articles