question

ShalvaGabriel-1621 avatar image
0 Votes"
ShalvaGabriel-1621 asked karenpayneoregon answered

How can I wait for the links to be created before doing something else ?

I have a class that download one file and create from it List of links.

I want to know when the list of links is ready and then do something with it.

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Text;
 using System.Threading.Tasks;
    
 namespace Extract
 {
     class Satellite
     {
         private List<string> satelliteUrls = new List<string>();
         private string mainUrl = "https://some.com/";
         private string[] statements;
    
         public async Task DownloadSatelliteAsync()
         {
             using (var client = new WebClient())
             {
                 client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");
    
                 client.DownloadFileCompleted += (s, e) =>
                 {
                     if(e.Error == null)
                     {
                         ExtractLinks();
                     }
                 };
    
                 await client.DownloadFileTaskAsync(new Uri(mainUrl), @"d:\Downloaded Images\Satellite\extractfile" + ".txt");
             }
         }
    
         private void ExtractLinks()
         {
             var file = File.ReadAllText(@"d:\Downloaded Images\images\extractfile" + ".txt");
    
             int idx = file.IndexOf("arrayImageTimes.push");
             int idx1 = file.IndexOf("</script>", idx);
    
             string results = file.Substring(idx, idx1 - idx);
             statements = results.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
    
             for (int i = 0; i < statements.Length; i++)
             {
                 if (i == 10)
                 {
                     break;
                 }
    
                 string time = statements[i].Split('\'')[1];
    
                 satelliteUrls.Add("https://some.com=" + time);
             }
         }
     }
 }


In Form1

 private async void btnStart_Click(object sender, EventArgs e)
         {
             lblStatus.Text = "Downloading...";
             await sat.DownloadSatelliteAsync();
         }

I want in Form1 to do something with the satelliteUrls List when the List is ready like for example download the files in the List.

The question is when and how do I know the list is ready in Form1 ? Should I use some while loop ? I want something async.

dotnet-csharpwindows-forms
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.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

I would try awaiting Task<bool> rather than void (untested)

 class Satellite
 {
     private List<string> satelliteUrls = new List<string>();
     private string mainUrl = "https://some.com/";
     private string[] statements;
    
     public async Task<bool> DownloadSatelliteAsync()
     {
         using var client = new WebClient();
         client.Headers.Add("user-agent", 
             "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + 
             "(KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");
    
         client.DownloadFileCompleted += DownloadFileCompleted;
    
         await client.DownloadFileTaskAsync(new Uri(mainUrl), 
             @"d:\Downloaded Images\Satellite\extractfile" + ".txt");
    
         return true;
     }
    
     private async void DownloadFileCompleted(object s, AsyncCompletedEventArgs asyncCompletedEventArgs)
     {
         if (asyncCompletedEventArgs.Error is null)
         {
             await ExtractLinksAsync();
         }
     }
    
     private async Task<bool> ExtractLinksAsync()
     {
         var file = await File.ReadAllTextAsync(@"d:\Downloaded Images\images\extractfile" + ".txt");
    
    
         int idx = file.IndexOf("arrayImageTimes.push", StringComparison.Ordinal);
         int idx1 = file.IndexOf("</script>", idx, StringComparison.Ordinal);
    
         string results = file.Substring(idx, idx1 - idx);
         statements = results.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
    
         for (int i = 0; i < statements.Length; i++)
         {
             if (i == 10)
             {
                 break;
             }
    
             string time = statements[i].Split('\'')[1];
    
             satelliteUrls.Add("https://some.com=" + time);
         }
    
         return true;
     }
 }
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.