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

Shalva Gabriel 61 Reputation points
2021-10-22T17:35:33.673+00:00

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.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,838 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,196 Reputation points
    2021-10-22T21:25:06.047+00:00

    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;
        }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful