Hi,

I have a app that at startup shows a particular label i.e.

Ready...

then when the user hit a download button it changes the label to:-

Contacting Server...

By using 'Threading' I have also added an update progress which updates the label with percentage and also updates a progressbar.

That as it stands all works fine, only that on completion of the download the label stays showing 100% and the progressbar is full.

I thought that just adding a

Thread.Sleep(2500);

function after this and then setting the label & progressbar back to default would sort it out but I was wrong.

Here is the code which activates after the button press:-

private void Startbtn_Click(object sender, EventArgs e)
        {
            if (StoreIDtb.Text == "")  // Check to see if there is data in the StoreID TextBox before proceeding
            {
                MessageBox.Show("You need to enter a Store ID before pressing to START button", "Enter Store ID", MessageBoxButtons.OK, MessageBoxIcon.Error);

                StoreIDtb.Focus();
            }

            else // If there is data in the StoreID TextBox then proceed
            {

                // Let the user know we are connecting to the server
                StatusBarlbl.Text = "Contacting Server...";
                // Create a new thread that calls the Download() method
                thrDownload = new Thread(new ParameterizedThreadStart(Download));
                // Start the thread, and thus call Download(); start downloading from the beginning (0)
                thrDownload.Start(0);
                

            }
        }

Here is the code for the update progress:-

private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
        {
            // Calculate the download progress in percentages
            PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
            // Make progress on the progress bar
            StatusBarProgressBar.Value = PercentProgress;
            // Display the current progress on the form

          StatusBarlbl.Text = "Getting Store Info" + " (" + PercentProgress + "%)";

         Thread.Sleep(2500);

              StatusBarProgressBar.Value = 0;
              StatusBarlbl.Text = "Ready...";
       
        }

What is actually hapening is thus;

At startup the label says 'Ready...', ProgressBar is empty.

When the button is clicked the label says 'Contacting Server...', the ProgressBar advances to 100.

After the 2.5 seconds of Thread.Sleep, the label defaults to 'Ready...' and the ProgressBar returns to it's empty state.

As you can see, it is completely missing out the step where the label should be displaying:-

'Getting Store Info '(x%)

Now if I remove the code form Thread.Sleep i.e.

private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
        {
            // Calculate the download progress in percentages
            PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
            // Make progress on the progress bar
            StatusBarProgressBar.Value = PercentProgress;
            // Display the current progress on the form

          StatusBarlbl.Text = "Getting Store Info" + " (" + PercentProgress + "%)";
 }

The 'Getting Store Info '(x%) is displayed.

How can I set the label & ProgressBar back to default after it has reached 100% without losing the above step?

Regards..,

MT

Instead of using a Thread use a BackgroundWorker.
This triggers events for:
DoWork - the main execution thread
ProgressChanged - triggered in the work method to update progress bars etc.
RunWorkerComplete - triggers at the end of the job.

MSDN- BackgroundWorker Class
This should suit your task nicely.:)

Instead of using a Thread use a BackgroundWorker.
This triggers events for:
DoWork - the main execution thread
ProgressChanged - triggered in the work method to update progress bars etc.
RunWorkerComplete - triggers at the end of the job.

MSDN- BackgroundWorker Class
This should suit your task nicely.:)

Hi Nick,

thanks for replying.

I did start off looking at using BackGroundWorker and did write a testbed application using it, although what I found was that where my file download code in the 'DoWork' procedure was held, it was causing the file to be downloaded over and over again until the end of the 'for loop' was reached.

I had no idea how to solve that, hence why I switched to 'Threading'.

Regards..,

MT

I would strongly recommend using the BackgroundWorker as this is by far the safest way to perform a background task.

Whatever code you have in your Thread method should move almost unchanged to the DoWork event handler of a BackgroundWorker.

A little bit of extra info on how (I think) it works.

The DoWork method is executed on a new thread.
The ProgressChanged and RunWorkerComplete methods are invoked to the thread that created the BackgroundWorker; since this is often the UI thread, UI controls can be updated in these methods without the need to Invoke.

I would strongly recommend using the BackgroundWorker as this is by far the safest way to perform a background task.

Whatever code you have in your Thread method should move almost unchanged to the DoWork event handler of a BackgroundWorker.

A little bit of extra info on how (I think) it works.

The DoWork method is executed on a new thread.
The ProgressChanged and RunWorkerComplete methods are invoked to the original calling thread (i.e. the thread that executes the RunWorkerAsync); since this is often the UI thread, UI controls can be updated in these methods without the need to Invoke.

Thanks Nick,

I'll have a whirl with that tomorrow, I've got square eyes now from staring at the monitor for soooo long.

Kind regards..,

MT

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.