INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

BackgroundWorker progress bar

BackgroundWorker progress bar

(OP)
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 --> c#

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";
        } 

RE: BackgroundWorker progress bar

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

RE: BackgroundWorker progress bar

(OP)
I am using Visual Studio 2012 Microsoft Visual C# Compiler version 4.6.1586.0 for C# 5

Thanks

RE: BackgroundWorker progress bar

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 --> C#

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.

RE: BackgroundWorker progress bar

(OP)
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.

RE: BackgroundWorker progress bar

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.

RE: BackgroundWorker progress bar

(OP)
Style of the progress bar set to "Blocks" = Yes
Enabled=true = Yes

RE: BackgroundWorker progress bar

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.

RE: BackgroundWorker progress bar

(OP)
500 records take about 23 seconds

RE: BackgroundWorker progress bar

(OP)
The label appears but the % is not updated either. When it is done the message box and label are updates fine

RE: BackgroundWorker progress bar

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close