-1

I m having a windows form class frmProcessSalary and inside setProgress method is implemented.

public partial class frmProcessSalary : Form
{
    public void setProgress()
    {
        int i;

        progressBar1.Minimum = 0;
        progressBar1.Maximum = 200;

        for (i = 0; i <= 200; i++)
        {
            progressBar1.Value = i;
        }
    }
}

From another class outside I create a reference to this and call this method.

frmProcessSalary newProcessSalary = new frmProcessSalary();
newFrmProcessSalary.setProgress();

In this outside class I have hundreds of queries being executed while calling this frmProcessSalary method. But the progress bar does not proceed.

I tried with Application.DoEvents() also but no success.

Any idea please?

M. Schena
  • 2,039
  • 1
  • 21
  • 29
code_Finder
  • 305
  • 1
  • 2
  • 15
  • I agree with Thorsten, I just wanted to suggest this http://stackoverflow.com/questions/12126889/how-to-use-winforms-progress-bar for you to know how to properly use the progress bar, background worker async, etc.. – Ilan Kutsman Feb 23 '16 at 10:59

2 Answers2

1

if you are running the queries in the main thread the UI will not response till the work is done. Try to use a BackgroundWorker.

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += DoWork;
bw.ProgressChanged += SetProgress;
bw.RunWorkerAsync()

In DoWork you have to call

bw.ReportProgress(percent)
  • What is this bw.ReportProgress(percent) about.Please detail – code_Finder Feb 23 '16 at 10:44
  • It's being done in a separate thread, so you have to report the result to the UI thread which shows the progress bar – Ilan Kutsman Feb 23 '16 at 10:47
  • The ReportProgress-Method triggers the ProgressChanged event and passes the progressvalue (a percentvalue) as a parameter. So in the SetProgress EventHandler you get the value to set in your progressBar. – Thorsten Hacke Feb 23 '16 at 10:48
0
public void setProgress()
           {

            int i;

            progressBar1.Minimum = 0;
            progressBar1.Maximum = 200;

            for (i = 0; i <= 200; i++)
            {
                progressBar1.Value = i;
                Application.DoEvents();
            }

        }