question

ShalvaGabriel-1621 avatar image
0 Votes"
ShalvaGabriel-1621 asked JackJJun-MSFT commented

How can I use sync task await to test downloading?

In form1 button click event :

First I'm trying to generate the Radar links.

 private async void btnStart_Click(object sender, EventArgs e)
         {
             lblStatus.Text = "Downloading...";
    
             await rad.GetRadarImages();
    
             await sat.DownloadSatelliteAsync();
             foreach(string link in sat.SatelliteUrls())
             {
                 urls.Add(link);
             }
             await DownloadAsync();
         }


The Radar class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
    
 namespace Extract
 {
     class Radar
     {
         private List<string> links = new List<string>();
         string defaultlink;
         DateTime current;
         string currentLink;
         public List<DateTime> dates = new List<DateTime>();
         DateTime workingFirstDateTime;
    
         public async Task GetRadarImages()
         {
             defaultlink = "mysite.com/Radar_";
    
             current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
    
             var ct = current.ToString("yyyyMMddHHmm");
             currentLink = defaultlink + ct + ".gif";
    
             using (System.Net.WebClient wc = new System.Net.WebClient())
             {
    
                 wc.DownloadFileCompleted += (s, e) =>
                 {
                     if (e.Error != null)
                     {       
                             current = current.AddMinutes(-5);
                             ct = current.ToString("yyyyMMddHHmm");
                             currentLink = defaultlink + ct + ".gif";
                             wc.DownloadFileTaskAsync(new Uri(currentLink), @"d:\Downloaded Images\Radar\radar0.gif");
                     }
                     else
                     {
                         workingFirstDateTime = current;
    
                         GenerateRadarLinks();
                     }
                 };
    
                 await wc.DownloadFileTaskAsync(new Uri(currentLink), @"d:\Downloaded Images\Radar\radar0.gif");
             }
         }
    
         private void GenerateRadarLinks()
         {
             for (var i = 0; i < 34; i++)
             {
                 current = current.AddMinutes(-5);
    
                 dates.Add(current);
                 var date = dates[i].ToString("yyyyMMddHHmm");
                 links.Add(defaultlink + date + ".gif");
             }
    
             dates.Insert(0, workingFirstDateTime);
         }
    
         DateTime RoundDown(DateTime date, TimeSpan interval)
         {
             return new DateTime(date.Ticks / interval.Ticks *
                 interval.Ticks);
         }
     }
 }


I'm downloading first time :

 await wc.DownloadFileTaskAsync(new Uri(currentLink), @"d:\Downloaded Images\Radar\radar0.gif");


Then in the completed event I'm checking if the downloaded file was fine or not by checking for error/s :

 if (e.Error != null)
                         {       
                                 current = current.AddMinutes(-5);
                                 ct = current.ToString("yyyyMMddHHmm");
                                 currentLink = defaultlink + ct + ".gif";
                                 wc.DownloadFileTaskAsync(new Uri(currentLink), @"d:\Downloaded Images\Radar\radar0.gif");
                         }
                         else
                         {
                             workingFirstDateTime = current;
        
                             GenerateRadarLinks();
                         }
                     };

If there is an error rebuild the link and try to download again this way I'm trying to rebuild the link over and over trying to download it until the download is success if there is no error/s the download is success then generate the link by calling the method GenerateRadarLinks()

The problem is I used a breakpoint on the line :

 if (e.Error != null)

I see error 404 not found so the link is incorrect it should try to download over again with the new built link but instead it's jumping to the Program.cs and throw there the exception 404 not found :

143076-notwait1.jpg



Why it's not trying to download the new built link over and over again until success ?

dotnet-csharpwindows-forms
notwait1.jpg (186.3 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ShalvaGabriel-1621, the error 404 not found may be caused for many reasons, you could check the following steps.

First, you could check if your uri is correct, please input the url directly to check if the files can be downloaded successfully.

Second, you could check if you have power to access the website.

0 Votes 0 ·

0 Answers