John P.
John P.

Reputation: 1249

Nested Async Download - Async within Async

I have some nested async methods calling each other and it is confusing. I am trying to convert a project which downloads the files in an async download. On the click of the download button this is the method triggered:

private async void enableOfflineModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < _playlists.Count; i++)
            {
                DoubleDimList.Add(new List<String>());
                for(int j = 0; j < 5; j++)
                {
                    string sMp3 = IniReadValue(_playlists[i], "Track " + j);
                    DoubleDimList[i].Add(sMp3);
                }
                await Task.Run(() =>  _InetGetHTMLSearchAsyncs(DoubleDimList[i]));              
            }
        }

It creates a 2d List which at the end looks like this DoubleDimList[3][20]. At the end of each sublist I am doing an async download as you can see. The method looks like this

private async Task _InetGetHTMLSearchAsyncs(List<string> urlList)
        {
            foreach (var url in urlList)
            {
                await Task.Run(() => _InetGetHTMLSearchAsync(url));
            }
        }

the _InetGetHTMLSearchAsync method looks like this and here is where it gets tricky

private async Task _InetGetHTMLSearchAsync(string sTitle)
        {
            Runs++;
            if (AudioDumpQuery == string.Empty)
            {
                //return string.Empty;
            }
            string sResearchURL = "http://www.audiodump.biz/music.html?" + AudioDumpQuery + sTitle.Replace(" ", "+");
            try
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add("Referer", @"http://www.audiodump.com/");
                    client.Headers.Add("user-agent", "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A");
                    client.DownloadStringCompleted += Client_DownloadStringCompleted;
                    await Task.Run(() => client.DownloadStringAsync(new Uri(sResearchURL)));

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Debug message: " + ex.Message + "InnerEx: " + ex.StackTrace);
                Console.WriteLine("Runs: " + Runs);
                return;
            }
        }

On Client_DownloadStringCompleted there is another async method called. Here it is

private async void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            string[] sStringArray;
            string aRet = e.Result;
            string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline);
            if (aTable != null)
            {
                string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\"");
                if (aInfos != null)
                {
                    for (int i = 0; i < 1; i++)
                    {
                        sStringArray = aInfos[i].Split('*');
                        sStringArray[0] = sStringArray[0].Replace("&#39;", "'");
                        aLinks.Add(sStringArray[0]);
                    }
                    await Task.Run(() => DownloadFile(aLinks[FilesDownloaded]));
                }
            }
        }

From there, surprise! Another async call.

private async Task DownloadFile(string url)
        {
            try
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add("Referer", @"http://www.audiodump.biz/");
                    client.Headers.Add("user-agent", "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A");
                    client.DownloadFileCompleted += Client_DownloadFileCompleted;
                    await Task.Run(() => client.DownloadFileTaskAsync(url, mp3Path + "\\" + count + ".mp3"));

                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine("File download error: " + Ex.StackTrace);
            }
        }

Now the first part after the creation of the 2d List is to retrieve the download links of the mp3s. The second part is to download the mp3 as soon as a valid URL was provided. It works but in a bizarre way. Instead of downloading the file normally(1st, 2nd, 3rd...), it will download randomly the files(1st, 5th, 8th...).

It is my first go for async download and boy, I am already far from my limits.

Where am I messing this up? And the main question, will this ever work the way it is supposed to work?

Upvotes: 0

Views: 274

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456507

Your code looks pretty good, except for two things:

  1. You shouldn't be using Task.Run. The primary use case for Task.Run is for moving CPU-bound work off a GUI thread so it doesn't block the UI. I have a series on the proper use of Task.Run that goes into this in detail.
  2. You should use a consistent asynchronous pattern, ideally TAP. Your code is currently using TAP everywhere except in _InetGetHTMLSearchAsync, which is using EAP. This is what is causing the odd behavior you're seeing.

A fixed _InetGetHTMLSearchAsync would look something like this:

private async Task _InetGetHTMLSearchAsync(string sTitle)
{
  Runs++;
  string sResearchURL = "http://www.audiodump.biz/music.html?" + AudioDumpQuery + sTitle.Replace(" ", "+");
  try
  {
    using (var client = new WebClient())
    {
      client.Headers.Add("Referer", @"http://www.audiodump.com/");
      client.Headers.Add("user-agent", "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A");
      string[] sStringArray;
      string aRet = await client.DownloadStringTaskAsync(new Uri(sResearchURL));
      string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline);
      if (aTable != null)
      {
        string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\"");
        if (aInfos != null)
        {
          for (int i = 0; i < 1; i++)
          {
            sStringArray = aInfos[i].Split('*');
            sStringArray[0] = sStringArray[0].Replace("&#39;", "'");
            aLinks.Add(sStringArray[0]);
          }
          await DownloadFile(aLinks[FilesDownloaded]); // Should really be called "DownloadFileAsync"
        }
      }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine("Debug message: " + ex.Message + "InnerEx: " + ex.StackTrace);
    Console.WriteLine("Runs: " + Runs);
    return;
  }
}

Upvotes: 1

Related Questions