首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多次调用DownloadFileAsync

多次调用DownloadFileAsync
EN

Stack Overflow用户
提问于 2015-08-21 12:00:40
回答 1查看 665关注 0票数 1

我在做一个WPF申请。我使用WebClient的DownloadFileAsync下载文件。我一次从一个文件夹下载每个文件。当我第一次调用DownloadProtocol时,它可以正常工作,但是当我想下载一个新文件夹时,我会再次调用DownloadProtocol来冻结我的应用程序。我希望在同一时间更多的下载过程中。

代码语言:javascript
复制
            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!");
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-21 12:27:46

我想一次下载一个文件

而且,在不阻塞您的UI的情况下,异步/等待是一个很好的选择。您可以编写一个方法DownloadFile

代码语言:javascript
复制
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);
    }
}   

把它用在这里

代码语言:javascript
复制
async void SomeClickHandler()
{
    var urls = new string[] {
        "http://google.com","http://stackoverflow.com"
    };

    foreach(var url in urls)
    {
        await DownloadFile(url, filename);
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32139790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档