c# - 多次调用 DownloadFileAsync

标签 c# wpf download webclient

我正在制作一个 WPF 应用程序。我使用 WebClient 的 DownloadFileAsync 下载文件。我一次从文件夹中下载每个文件。我第一次调用 DownloadProtocol 它工作正常,但是当我想下载一个包含新文件的新文件夹时,我再次调用 DownloadProtocol 我的应用程序卡住了。我希望同时进行更多下载。

            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!");
        }
    }

最佳答案

i want to download one file at a time

在不阻塞您的 UI 的情况下,async/await 非常适合这种情况。你可以写一个方法 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);
    }
}   

像这里一样使用它

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

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

关于c# - 多次调用 DownloadFileAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32139790/

相关文章:

wpf - 自定义控件不继承父级的样式

java - 无法从Android中的ftp服务器下载txt文件

file - Cakephp 2.3.x发送文件并强制下载mp4文件

c# - 如何使 WPF 组合框具有其在 XAML 中最宽元素的宽度?

c# - .NET Compact Framework 3.5 是否有 BackgroundWorker 替代品?

c# - 为什么 catch 语句不捕获所有异常?

javascript - 触发文件下载的正确方法?

c# - 具有 Azure 辅助角色的 Ninject

c# - 从方法返回更新

c# - 有没有办法将 .mp4 格式音频转换为内存中的 .wav 格式?