Packman.InstallablePackage.DownloadFilesAsync C# (CSharp) Method

DownloadFilesAsync() private method

private DownloadFilesAsync ( string downloadDir ) : System.Threading.Tasks.Task
downloadDir string
return System.Threading.Tasks.Task
        internal async Task DownloadFilesAsync(string downloadDir)
        {
            if (Directory.Exists(downloadDir))
                return;

            var list = DownloadFiles(downloadDir, Files);

            OnDownloading(downloadDir);
            await Task.WhenAll(list);

            var remaining = AllFiles.Where(f => !Files.Contains(f));

            if (remaining.Any())
            {
                // Fire and forget to return call immediately. Download the rest of the files
                // TODO: Is there a more elegant way than using the threadpool like this?
                System.Threading.ThreadPool.QueueUserWorkItem(async (o) =>
                {
                    OnDownloadingRemainingFiles(downloadDir);

                    await Task.WhenAll(DownloadFiles(downloadDir, remaining));
                });
            }
        }

Usage Example

Example #1
0
        async Task <bool> CopyPackageContent(InstallablePackage entry, InstallSettings settings)
        {
            string cachePath  = Environment.ExpandEnvironmentVariables(Defaults.CachePath);
            string versionDir = Path.Combine(cachePath, Provider.Name, entry.Name, entry.Version);
            bool   hasCopied  = false;

            await entry.DownloadFilesAsync(versionDir);

            await Task.Run(() =>
            {
                try
                {
                    foreach (string file in entry.Files)
                    {
                        string cleanFile = file.Replace("/", "\\");
                        string src       = Path.Combine(versionDir, cleanFile);
                        string dest      = Path.Combine(settings.InstallDirectory, cleanFile);

                        if (File.Exists(dest))
                        {
                            var srcDate  = File.GetLastWriteTime(src);
                            var destDate = File.GetLastWriteTime(dest);

                            if (srcDate == destDate)
                            {
                                continue;
                            }
                        }

                        string dir = Path.GetDirectoryName(dest);
                        Directory.CreateDirectory(dir);

                        OnCopying(src, dest);
                        File.Copy(src, dest, true);
                        OnCopied(src, dest);
                        hasCopied = true;
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        Directory.Delete(versionDir, true);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Write(ex);
                    }
                }
            });

            return(hasCopied);
        }
All Usage Examples Of Packman.InstallablePackage::DownloadFilesAsync