Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BackgroundWorker progress bar

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
0
0
US
I have the code below and I am trying to get the progress bar to update as the files are moved. SO far no luck I am not sure what I am missing or what I have wrong with the code. Any help is appreciated. I am trying to move 500 records for testing purpose

Thanks
RJL

Code:
public WEPK_Data_Mover()
        {
            InitializeComponent();
        }

        private void WEPK_Data_Mover_Load(object sender, EventArgs e)
        {
            percentagelabel.Text = "";
            PrepareList();
        }

        private void PrepareList()
        {
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            cmd.CommandText = "SP_Move_Shipments_List";
            cmd.CommandType = CommandType.StoredProcedure;

            cs.Open();
            cmd.Connection = cs;
            reader = cmd.ExecuteReader();

            try
            {
                rowCount = 0;

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        rowCount++;
                        string _InternalNum = (reader.GetString(reader.GetOrdinal("INTERNAL_NUM")));
                        FileList.Add(_InternalNum);
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            reader.Close();
            cs.Close();
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < rowCount; i++)
            {   
                foreach (string _Record in FileList)
                {
                    _IntNum = _Record;

                    SqlCommand cmd = new SqlCommand();
                    SqlDataReader reader;
                    SqlParameterCollection sqlParameters = (SqlParameterCollection)cmd.Parameters;

                    sqlParameters.AddWithValue("@INTERNAL_SHIPMENT_NUM", _IntNum);

                    cmd.CommandText = "SP_Move_Shipments";
                    cmd.CommandType = CommandType.StoredProcedure;

                    cs.Open();
                    cmd.Connection = cs;
                    reader = cmd.ExecuteReader();

                    reader.Close();
                    cs.Close();

                    int percentage = (i + 1) * 100 / rowCount;
                    backgroundWorker1.ReportProgress(percentage);
                }

                FileList.Clear();
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            percentagelabel.Text = "Moving Shipments " + e.ProgressPercentage.ToString() + " % completed";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            percentagelabel.Text = "Task completed " + rowCount + " records moved";
        }
 
What version of C# are you using?

Best Regards,
Scott
MIET, MASHRAE, CDCP, CDCS, CDCE, CTDC, CTIA, ATS

"Everything should be made as simple as possible, and no simpler."[hammer]
 
I am using Visual Studio 2012 Microsoft Visual C# Compiler version 4.6.1586.0 for C# 5

Thanks
 
What exactly is "not working"?
Have you set the "ReportsProgress" property of the worker to true?
Is the progressbar just moving too slowly?

In case of the latter:
Code:
if(e.ProgressPercentage<100) 
{
  progressBar1.Value = e.ProgressPercentage + 1;
  progressBar1.Value--;
}

Explanation: if the value of the progress bar increases, the blue bar is animated to that value. This takes time. If the valaue decreases, the bar is not animated but jumps directly tothat value. So increasing it to a value one too high and then immediately decreasing it by one again makes the bar jump to the desired value practically without animation.
Very good for fast processes.



"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Sorry forgot to specify. The list is prepared OK and the stored procedure to move the files executes but the progress bar never shows any progress at all.
 
Style of the progress bar set to "Blocks"? Enabled=true?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Style of the progress bar set to "Blocks" = Yes
Enabled=true = Yes
 
How long does your loop run, in seconds, roughly?
Does your percentagelabel update correctly, showing the percentage progress?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
500 records take about 23 seconds
 
The label appears but the % is not updated either. When it is done the message box and label are updates fine
 
Something is not correct here.
To be on the safe side, please change this:
Code:
public WEPK_Data_Mover()
{
     InitializeComponent();
}
to this:
Code:
public WEPK_Data_Mover()
{
     InitializeComponent();
     backgroundWorker1.WorkerReportsProgress = true;
}

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top