0

I need the progress bar running while submit button is clicked and to freeze the screen for some 5 seconds but the code in the submit button should do the work in parallel.

I have created a wait form but I have called the wait form once the connection is closed so that progress bar is taking time to appear and I can select the other options in the screen which is shouldn't.

private void cmdselect_click(System.object _sender System.EventArgs e1)
{
    using (frmwait frm=new frmwait(savedata)) //  and I have called wait form here
    {
        frm.Showdialog(this)
    }
}

// Savedata method

void Savedata()
{
    for(int i=1;i<=500;i++)
    {
        Thread.Sleep(10);
    }
}

Progress bar is not starting immediately once the button click is fired.

theduck
  • 2,589
  • 13
  • 17
  • 23
Fouzan Khan
  • 5
  • 1
  • 4
  • I am currently using a pop up progress bar that I created after watching this video: - https://www.youtube.com/watch?v=yZYAaScEsc0. I just realised I shared my code for it in this stackoverflow answer: - https://stackoverflow.com/questions/57377016/how-to-load-the-progressbar-at-the-same-time-my-method-is-running – ZedLepplin Nov 05 '19 at 09:11
  • I have done the same by watching this. – Fouzan Khan Nov 05 '19 at 09:34
  • I went back and looked at my code and I call the progress bar form exactly the same as you do. Mine appears instantly so I think the problem must be in the code for your progress bar form. – ZedLepplin Nov 05 '19 at 11:39

1 Answers1

0

You can use this:

progressBar.Minimum = 0;
progressBar.Maximum = 500;
progressBar.Value = 0;
progressBar.Step = 1;
progressBar.Refresh();

Enabled = false;               // for UX
Cursor = Cursors.WaitCursor;   // for UX
try
{
  for( int i = 1; i <= progressBar.Maximum; i++ )
  {
    // do process
    progressBar.PerformStep();
    progressBar.Refresh();
    Thread.Sleep(10);
  }
}
finally
{
  Cursor = Cursors.Default;
  Enabled = true;
}

No need to Sleep unless you want to reduce the speed of the process.

  • while hitting progressbar.Value=0 its throws an system.argumentnullexception saying value cannot be null. – Fouzan Khan Nov 05 '19 at 08:05
  • How do you create the bar? By the designer while drop it on the form or by code and is it created before usage? –  Nov 05 '19 at 08:10
  • by designer i have in another form i am trying to access it. – Fouzan Khan Nov 05 '19 at 08:11
  • Have you changed the other form constructor? It must have InitializeComponents call at first. –  Nov 05 '19 at 08:46
  • Yes right what i have to change there I have intializecomponent() if (worker==null) {throw argument null exception} – Fouzan Khan Nov 05 '19 at 09:27
  • You should avoid throwing exception in constructor or you must manage the case in a try catch either in the constructor either when calling new Form. –  Nov 05 '19 at 09:34
  • I have change the style .I have kept Marquee it is saying perform step cannot be performed in that style. – Fouzan Khan Nov 05 '19 at 09:43
  • Yes, steps can't be used with marquee that is only an animation without min and max ans setp: https://stackoverflow.com/questions/312936/windows-forms-progressbar-easiest-way-to-start-stop-marquee –  Nov 05 '19 at 09:46
  • the code is working fine but progress bar is not displaying. – Fouzan Khan Nov 05 '19 at 09:58
  • Can you make a link to a zip of the project folder? –  Nov 05 '19 at 10:06
  • I work in VMware we cant copy the code or zip thats the issue. – Fouzan Khan Nov 05 '19 at 10:11
  • Do you show the form? Where is the bar in the form? Is the Visible property on? –  Nov 05 '19 at 10:12
  • I have progress bar in another form i Called from there frmwait.progressbar1.show(); but it is not displaying and also i have visible to true. – Fouzan Khan Nov 05 '19 at 10:38
  • Have you called frmwait.Show() ? –  Nov 05 '19 at 10:39
  • Yeah i have called now but the background doesnt freeze also progress bar keeps progressing it doesnt hide. – Fouzan Khan Nov 05 '19 at 10:52
  • Thanks a lot for your help.Lastly if i use form.show() its working fine but form.showdialog its getting struck but I have freeze parent form – Fouzan Khan Nov 05 '19 at 14:12
  • Yes, ShowDialog() wait to Close() before returning to the caller. So to show a splash you need to use Show() and you can also disable the caller form by using this.Enabled = false for example like in the answer updated. –  Nov 05 '19 at 14:38
  • Its working perfectly but one issue is that whenever i click on parent form the processing form(child form) gets minimized anyway to overcome this ? – Fouzan Khan Nov 06 '19 at 07:52
  • You can set the splash form `TopMost` property to `true`. Hence if the user click on the background caller form the splash will remain on top. –  Nov 06 '19 at 09:09
  • Thanks a lot for your help it all went fine :) – Fouzan Khan Nov 06 '19 at 12:23