Loc Dai Le
Loc Dai Le

Reputation: 1697

Call DownloadFileAsync multiple times

I'm making a WPF application. I use WebClient's DownloadFileAsync to download files. I Download each file at a time from a folder. First time I call DownloadProtocol it works fine, but when i want to download a new folder with new files i then Call DownloadProtocol again my application freezes. I want more download in process in the same time.

            foreach (var x in res)

            {
                Console.WriteLine("111");
                await DownloadProtocol("http://cdn.something.com/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);

            }

  public async Task DownloadProtocol(string address, string location)
    {

        Uri Uri = new Uri(address);
        using (WebClient client = new WebClient())
        {
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
            await client.DownloadFileTaskAsync(Uri, location);
        }
        /*while (client.IsBusy)
        {
            DoEvents();
        }*/
    }

    public void DoEvents()
    {
        DispatcherFrame frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            new DispatcherOperationCallback(ExitFrame), frame);
        Dispatcher.PushFrame(frame);
    }
    public object ExitFrame(object f)
    {
        ((DispatcherFrame)f).Continue = false;

        return null;
    }

    private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
    {
        // Displays the operation identifier, and the transfer progress.
        Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
            (string)e.UserState,
            e.BytesReceived,
            e.TotalBytesToReceive,
            e.ProgressPercentage);
    }

    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            Console.WriteLine("Download has been canceled.");
        }
        else
        {

            Console.WriteLine("Download completed!");
        }
    }

Upvotes: 1

Views: 767

Answers (1)

Eser
Eser

Reputation: 12546

i want to download one file at a time

And without blocking your UI right?, async/await is a good fit for this. You can write a method DownloadFile

public async Task DownloadFile(string url, string fileName)
{
    using (WebClient client = new WebClient())
    {
        client.DownloadProgressChanged += (o, e) =>
        {
            //Console.WriteLine(e.BytesReceived);
        };
        await client.DownloadFileTaskAsync(url, filename);
    }
}   

and use it like here

async void SomeClickHandler()
{
    var urls = new string[] {
        "http://google.com","http://stackoverflow.com"
    };

    foreach(var url in urls)
    {
        await DownloadFile(url, filename);
    }
}

Upvotes: 3

Related Questions