-1

I'm trying to download the page source from multiple urls using tasks to download multiple sites at one time. The issue is that I want to keep the UI updated as each individual task completes. When I try to wait all tasks it stops updating the UI until they all finish. Here is the current code that I am using.

EDIT: I'm assuming I was down voted due to me not explaining well enough. I guess a better way to put this is why is the continueWith not being run before Task.WaitAll. I want the UI to update on each completion of the source being downloaded. Once that is all finished then the listbox would be updated to let the user know everything is done.

   private void btnGetPages_Click(object sender, EventArgs e)
    {

        for (int i = 1; i < 11; i++)
        {               
            string url = $"http://someURL/page-{i}.html";
            listBoxStatus.Items.Add($"Downloading source from {url}...");

            Task t = new Task(() =>
            {
                DownloadSource(url);
            });

            t.ContinueWith(prevTask => listBoxStatus.Items.Add($"Finished Downloading {url} source..."), TaskScheduler.FromCurrentSynchronizationContext());
            tasks.Add(t);
            t.Start();
        }

        Task.WaitAll(tasks.ToArray());
        listBoxStatus.Items.Add("All Source files have completed...");

    }

    private void DownloadSource(string url)
    {
        var web = new HtmlWeb();
        var doc = web.Load(url);
        pageSource += doc.Text;
    }
Timg
  • 227
  • 1
  • 3
  • 12
  • What .NET Framework version are you using? What version of C# are you using? Where did you see it is a good practice to create an instance of `Task` using the constructor and invoking `Start` on it? – Paulo Morgado Jan 08 '19 at 23:26
  • Possible duplicate of [How can I efficiently update the UI from an async method?](https://stackoverflow.com/questions/38247499/how-can-i-efficiently-update-the-ui-from-an-async-method) – JSteward Jan 09 '19 at 01:14
  • @PauloMorgado I am using .net 4.6.1 Visual C# win forms app. I have just been browsing and reading different ways to do tasks. I'm pretty new to C# and not really wrapping my head around the TPL and how it all comes together. – Timg Jan 09 '19 at 05:33
  • What version of C#/Visual Studio are you using? – Paulo Morgado Jan 09 '19 at 09:08
  • @PauloMorgado I am using VS 2017 community – Timg Jan 09 '19 at 17:43
  • I'll assume C#7.0, then. – Paulo Morgado Jan 09 '19 at 22:28

1 Answers1

0

You really should use an asynchronous download method based on HttpClient instead of the synchronous method you are showing. Lacking that, I'll use this one:

private async Task DownloadSourceAsync(string url)
{
    await Task.Run(() => DownloadSource(url));

    listBoxStatus.Items.Add($"Finished Downloading {url} source...");
}

Then, you can make your btnGetPages_Click method something like this:

private async void btnGetPages_Click(object sender, EventArgs e)
{
    var tasks = new List<Task>();

    for (int i = 1; i < 11; i++)
    {
        string url = $"http://someURL/page-{i}.html";
        listBoxStatus.Items.Add($"Downloading source from {url}...");

        tasks.Add(DownloadSourceAsync(url));
    }

    Task.WaitAll(tasks.ToArray());
    listBoxStatus.Items.Add("All Source files have completed...");
}
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • Just so I can clarify... When I add the tasks to the list that are calling the async method that will start them as well? – Timg Jan 10 '19 at 02:37
  • I used the code that you provided and It doesn't seem to be running the tasks. It adds the message to the listbox and then just does nothing. I stepped through the code and it never seems to get to the downloadSourceAsync method. – Timg Jan 10 '19 at 03:56
  • Have you tried to set a breakpoint on `DownloadSource` and `DownloadSourceAsync` to se if it really gets there? – Paulo Morgado Jan 10 '19 at 08:12
  • Yes it is getting to both methods however its hanging on the await Task.Run(). I checked that the source is in the var that I have setup and indeed it is so I know it has completed the download and reached the site. – Timg Jan 10 '19 at 18:29