Sign in to follow this  
Followers 0
AutID

Nested Asynchronous Downloads

1 post in this topic

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);
		 //throw exception
                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);
		 //throw exception
            }
        }

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...).

Where am I messing this up because I definitely am!?

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • drego
      By drego
      It's been requested in the past to have multithreading to which the response was "It would take too much redesigning of Autoit" but what about Async? Multithreading and Async are two different things. This way we could put tasks in the background without having to fork processes. Right? Also better event handling would be nice rather than throwing everything in a while loop we could have some functionality like javascript which seems to be far more responsive and reliable as the more you add to your while loop the less change there is of your "event" getting caught for some reason (At least in my experience).