0

I have azure A4 basic machine (8 core) to test my application:

namespace DownloadSomeData 
{
   internal class FileDownloader
   {
    int nextIndex = 0;
    int _ListSize;
    readonly uint MaxConcurrentDownloads = 0;
    private IList<MyModel> _ValidList;

    public FileDownloader(uint maxConcurrentThread, IList<MyModel> list)
    {
        _ValidList = list;
        MaxConcurrentDownloads = maxConcurrentThread;
        //Get the items count
        _ListSize = _ValidList.Count();

    }
    /// <summary>
    /// 
    /// </summary>
    public async Task Start()
    {
        uint parallelThreads = MaxConcurrentDownloads;

        if (_ListSize < MaxConcurrentDownloads)
        {
            parallelThreads = (uint)_ListSize;
        }
        // Now, start the downloads.
        for (int i = 0; i < parallelThreads; ++i)
        {
            var client = GetNextWebClient();
            MyModel nextObject = _ValidList.ElementAt(i);
            StartDownload(client, nextObject);
            nextIndex++;
        }

    }

    private WebClient GetNextWebClient()
    {
        var client = new WebClient();
        client.DownloadProgressChanged += DownloadProgressChanged;
        client.DownloadFileCompleted += DownloadFileCompleted;
        return client;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="client"></param>
    private void StartDownload(WebClient client, MyModel state)
    {
        try
        {
            string fname = state.ImageFilename;
            state.DownloadStatus = EnumDownloadStatus.Started;
            client.DownloadFileAsync(new Uri(state.ImageUri), fname, state);  // start the asynchronous download.
        }
        finally
        {
            if (client != null)
            {
                client.Dispose();
            }
        }

    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
    // Progress tracking here
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        var model = (MyModel)e.UserState;
        if (e.Error != null)
        {
        }
        else if (e.Cancelled == true)
        {

        }
        else
        {
        }
        var index = nextIndex;
        if (index < _ListSize)
        {
            MyModel nextObject = _ValidList.ElementAt(index);
            nextIndex = nextIndex + 1;
            StartDownload(GetNextWebClient(), nextObject);
        }
    }
}}

this code always do 2 or max 3 parallel download of files. I get the same behaviour even on i5 (4 core) machine. In-fact I run few other application on my i5 machine. but azure machine has no other program running except this.

I call this code using:

    private async Task StartDownload
    {
        FileDownloader download = new FileDownloader(_ConcurrentThread, _List);
        await download.Start();
    }

Have I am not written the write logic to take the max cores being used for the application?

user3378165
  • 6,546
  • 17
  • 62
  • 101
dotnetavalanche
  • 804
  • 2
  • 12
  • 25

0 Answers0