Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我只能用.NET GDrive API v3同时下载两次?_C#_.net_Wpf_Google Drive Api - Fatal编程技术网

C# 为什么我只能用.NET GDrive API v3同时下载两次?

C# 为什么我只能用.NET GDrive API v3同时下载两次?,c#,.net,wpf,google-drive-api,C#,.net,Wpf,Google Drive Api,我不确定这是一个客户端还是一个API限制,但我似乎不能用.NET GDrive API进行超过2次的并发下载。这给我的应用程序带来了一个大问题,因为如果用户想一次下载2个以上的文件,如果前2次下载没有及时完成,所有其他“排队”下载最终都会超时,这也会引发系统.Threading.Tasks.TaskWasCanceled,在我的例子中,因为我使用的是filesource.GetRequest.downloadsync 是否有某种属性可以更改为允许2次以上的并发下载,或者在引发异常之前增加超时 以

我不确定这是一个客户端还是一个API限制,但我似乎不能用.NET GDrive API进行超过2次的并发下载。这给我的应用程序带来了一个大问题,因为如果用户想一次下载2个以上的文件,如果前2次下载没有及时完成,所有其他“排队”下载最终都会超时,这也会引发
系统.Threading.Tasks.TaskWasCanceled
,在我的例子中,因为我使用的是
filesource.GetRequest.downloadsync

是否有某种属性可以更改为允许2次以上的并发下载,或者在引发异常之前增加超时

以下是我当前的下载功能:

public static async Task DownloadFile(string name, string downloadPath, string extractPath, string appExePath, GridButton clickedGridBtn)
{
    name += ".zip";
    downloadPath += ".zip";

    foreach (Google.Apis.Drive.v3.Data.File gDriveFile in filesList)
    {
        if (!gDriveFile.Name.Equals(name))
            continue;

        Utils.CreateDirIfNotExist(ConfigUtils.downloadPath);

        clickedGridBtn.Btn.IsEnabled = false;

        DownloadProgressPanel dlProgressPanel = new DownloadProgressPanel() { AppIconPath = $"/Resources/Icons/{name.Replace(".zip", string.Empty)}" };
        UIElementCollection dlProgressWindowChildren = MainWindow.Instance.DlProgressPanelStack.Children;
        bool alreadyExists = false;

        foreach (DownloadProgressPanel dlPanel in dlProgressWindowChildren)
        {
            if (dlPanel.AppIconPath.Equals(dlProgressPanel.AppIconPath))
            {
                int index = dlProgressWindowChildren.IndexOf(dlPanel);

                dlProgressWindowChildren.RemoveAt(index);
                dlProgressWindowChildren.Insert(index, dlProgressPanel);

                alreadyExists = true;
                break;
            }
        }

        if (!alreadyExists)
            dlProgressWindowChildren.Add(dlProgressPanel);

        MainWindow.Instance.TabButton_Click(MainWindow.Instance.DownloadsTabBtn);

        FileStream dlFileStream = new FileStream(downloadPath, FileMode.OpenOrCreate);

        FilesResource.GetRequest fileReq = service.Files.Get(gDriveFile.Id);
        long? fileSize = gDriveFile.Size;

        dlProgressPanel.DownloadSizeText.Text = $"Downloading... 0 MB of {Utils.ConvertToGigabytes(fileSize)}";
        dlProgressPanel.DLProgressBar.Value = Utils.ConvertToPercentage(0.0, (double)fileSize);

        fileReq.MediaDownloader.ChunkSize = 204800;
        fileReq.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
        {
            switch (progress.Status)
            {
                case DownloadStatus.Downloading:
                    UpdateProgressValues(progress, fileSize, dlProgressPanel);
                    break;
                case DownloadStatus.Completed:
                    UpdateProgressValues(progress, fileSize, dlProgressPanel);

                    dlFileStream.Dispose();
                    dlFileStream.Close();

                    DownloadCompleted(dlProgressPanel, clickedGridBtn, downloadPath, extractPath, appExePath);
                    break;
                case DownloadStatus.Failed:
                    Console.WriteLine($"Failed to download \"{name}\"!");
                    break;
            }
        };

        await fileReq.DownloadAsync(dlFileStream);
        break;
    }
}

如果我需要提供任何其他细节,请告诉我

看起来您可以设置服务的
HttpClient.Timeout
属性,该属性修复了我的问题。我仍然不能同时下载超过2次,但这已经足够了。我的主要问题是我的函数引发TaskWasCancelled异常。如果有人知道如何允许2次以上的并发下载,请对此答案发表评论!无论哪种方式,这里都有一个设置超时的示例(假设
service
是您的新
DriveService
实例):

编辑:感谢@Evk提供的并发下载修复。在应用程序启动时设置
ServicePointManager.DefaultConnectionLimit
属性似乎可以正常工作,例如:

ServicePointManager.DefaultConnectionLimit = 4;

你看过文件了吗?通过同一个应用程序,您可能只能同时下载几次。如果是这样,我们就帮不了你。然而,就像这个世界上的一切一样,你可能会给他们一些钱,然后增加。但那是speculative@TheGeneralAPI缺少大量文档,因此没有太多可查看的内容。我不确定这是客户端限制还是API限制,这就是我为什么在这里问的原因,也许有人知道,也许我需要在某个地方设置一个属性,指定允许的最大连接/下载,我没有任何线索。我是API新手,除了以前遇到过这个问题的人,或者比我更了解API的人之外,我真的没有其他地方可以从他们那里得到帮助。可能是这样的:。对于非asp.net应用程序,默认值为2,请尝试在开始时将其设置为更大的值application@Evk非常感谢,它很有效!我从来没有想过这个lol。
ServicePointManager.DefaultConnectionLimit = 4;