Daniel Halfoni Daniel Halfoni - 9 months ago 27
C# Question

Where should i change the labels visible to true in the code?

private void downloadFile(IEnumerable<string> urls)
{
foreach (var url in urls)
{
_downloadUrls.Enqueue(url);
}

// Starts the download
btnStart.Text = "Downloading...";
btnStart.Enabled = false;
pBarFileProgress.Visible = true;

label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
label7.Visible = true;

DownloadFile();
}

private void DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;

url = _downloadUrls.Dequeue();

if (url.Contains("animated") && url.Contains("infra"))
{
string startTag = "animated/";
string endTag = "/infra";

int index = url.IndexOf(startTag);
int index1 = url.IndexOf(endTag);

fname = url.Substring(index + 9, index1 - index - 9);
var countryName = codeToFullNameMap[fname];
downloadDirectory = tbxMainDownloadPath.Text;
downloadDirectory = Path.Combine(downloadDirectory, countryName);
}
else
{
fname = "Tempfile";
downloadDirectory = tbxMainDownloadPath.Text;
}

client.DownloadFileAsync(new Uri(url), downloadDirectory + "\\" + fname + ".gif");

lastDownloadedFile = downloadDirectory + "\\" + fname + ".gif";
label4.Text = downloadDirectory + "\\" + fname + ".gif";
return;
}

// End of the download
label2.Text = "All files have been downloaded";
}

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
// handle error scenario
throw e.Error;
}
if (e.Cancelled)
{
// handle cancelled scenario
}

if (url.Contains("animated") && url.Contains("infra"))
{
Image img = new Bitmap(lastDownloadedFile);
Image[] frames = GetFramesFromAnimatedGIF(img);
foreach (Image image in frames)
{
countFrames++;
image.Save(downloadDirectory + "\\" + fname + ".gif");
}
}

label2.Text = "Download Complete";
tracker.NewFile();
DownloadFile();
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
tracker.SetProgress(e.BytesReceived, e.TotalBytesToReceive);
pBarFileProgress.Value = (int)(tracker.GetProgress() * 100.0);
label3.Text = e.BytesReceived + "/" + e.TotalBytesToReceive;
label7.Text = tracker.GetBytesPerSecondString();
label2.Text = "Downloading";
}


The start download button

private void btnStart_Click(object sender, EventArgs e)
{
downloadFile(lines);
}


Now I'm changing the labels visible property to true when I click the download button. But then the I see labels for some seconds before it's getting to the progressChanged event and update the labels.

The question is where should I change the visible to true of this labels 2,3,4,7 and that it will change it once to true ?

Answer Source

If you're concerned about showing the labels before you actually set any text to them, you should just make sure to set text to them before you get the progress changed event - e.g.

label3.Text = "0/calculating...";
label7.Text = "0 kbps";
label2.Text = "Preparing for download...";

in your downloadFiles method (before or after setting .Visible to true). On another note - pick better names for your labels! label3 might be better as something like lblTotalDownloaded, or label7 as lblDownloadSpeed, etc.