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