c# - 下载异步错误。在意外的时间调用了一个方法

标签 c# windows-8 windows-runtime download

在我的 Windows 8 (c#) 应用程序中,我使用此结构来下载文件。

  var tasksDownload = listLinks.Select(link => DownloadAsync(new Uri(link, UriKind.RelativeOrAbsolute), folder, fileName)).ToArray();
        await Task.WhenAll(tasksDownload);

  public static async Task DownloadAsync(Uri fileUri, StorageFolder folder, string fileName)
    {
        try
        {
            var file = await folder.CreateFileAsync(fileName);
            var downloader = new BackgroundDownloader();
            var download = downloader.CreateDownload(fileUri, file);
            await download.StartAsync();
        }
        catch (InvalidOperationException e)
        {
            Debug.WriteLine("DownloadAsync error.  " + e.Message);
        }
    }

但是,当文件数为1123时,我有这样的异常(exception):

A first chance exception of type 'System.InvalidOperationException' occurred in MyApp.exe
WinRT information: Quota for maximum number of concurrent operations exceeded.
Wait for an operation to complete before starting new ones.
DownloadAsync error. A method was called at an unexpected time.

我想知道:

  1. 为什么会发生这种情况?
  2. Release模式解决了这个问题吗?
  3. 如果否,我可以同时下载多少个文件?
  4. 您知道另一种解决方案吗(不仅加载部分文件,然后加载下一部分......)

更新

现在,我将列表与小列表的文件链接分开,并尝试逐段下载,但这并不能解决问题。 当文件数量较小(<20)时也会抛出此异常。

最佳答案

就我个人而言,我要做的是创建一个 List ,然后在每个线程上运行每个 block 。这样,任何线程都不会超出限制。也可能会提高速度。如果需要,我可以提供代码。

关于c# - 下载异步错误。在意外的时间调用了一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298792/

相关文章:

mvvm-light - 在Windows 8中使用MVVMLight通过ViewModel进行页面导航

C# - WinRT - 将 IPv4 地址从 uint 转换为字符串?

https - HttpClient->GetStringAsync() 对于 https Uris 抛出 0x000006F4

c# - 使用 iTextSharp 将 HTML 样式(虚线下划线)转换为 PDF

windows-8 - 触摸时 FlipView 崩溃

Java 将 ArrayList 转换为 C#

c# - 在 XAML 中使用滚动查看器时动画中的图形闪烁/错误

c# - 在 MapItemsControl.ItemTemplate 中绑定(bind)网格的 EntranceThemeTransition 属性不起作用

c# - 转换时拆分 DateTime 字符串

c# - 未处理的异常 : System. AccessViolationException:尝试读取或写入