In a class I'm downloading a file and if the file is not downloaded fine with error it will try to download it again and if it's fine it will do something else :
using (System.Net.WebClient wc = new System.Net.WebClient())
{
wc.DownloadFileAsync(new Uri(currentLink), folders[0] + "\\image0.gif");
wc.DownloadFileCompleted += (s, e) =>
{
if (e.Error != null)
{
current = current.AddMinutes(-5);
ct = current.ToString("yyyyMMddHHmm");
currentLink = defaultlink + ct + ".gif";
wc.DownloadFileAsync(new Uri(currentLink), folders[0] + "\\image0.gif");
}
else
{
workingFirstDateTime = current;
GenerateRadarLinks();
}
};
}
In Form1 when I click the button to start downloading I'm calling this method :
private void btnStart_Click(object sender, EventArgs e)
{
dates = new List<DateTime>();
datesonimages = new List<string>();
btnRadarPath.Enabled = false;
btnSatellitePath.Enabled = false;
timeCounter1._timer.Enabled = true;
DownloadImages();
}
public async void DownloadImages()
{
CreateDownloadFolders();
urls = new List<string>();
lblStatus.Text = "Downloading...";
rad.GetRadarImages();
await Task.Delay(15000);
foreach (string link in rad.links)
{
urls.Add(link);
}
await sat.DownloadSatelliteAsync();
foreach (string link in sat.SatelliteUrls())
{
urls.Add(link);
}
urlsCounter = urls.Count;
await DownloadAsync();
}
And in the DownloadImages method I'm using this line to wait for the class to build the links I'm waiting until the first file will be downloading fine without errors then it's building links then I'm adding the links from the class to the urls List
await Task.Delay(15000);
So I'm waiting 15 seconds. In most of the cases it's working because the first file is downloading successfully before 15 seconds but this is not a real solution not a logic solution. Who said it will take 15 seconds until the file to be downloaded fine without error ? Maybe it will try to download the file for a minute or 5 seconds.
I mean 15 seconds is enough but it's not a solution. it's more kind of workaround.
I wonder if there is a better way to wait in form1 before continue ?