Ejemplo n.º 1
0
        private static void DownloadFileVersion2(DownloadableFile file, FileStream destination)
        {
            var tasks = new List<Task>();

            foreach (int i in Enumerable.Range(0, file.ChunkCount))
            {
                int chunkIndex = i;

                tasks.Add(Task.Run(async delegate
                {
                    byte[] chunkContents;

                    await _concurrentDownloadSemaphore.WaitAsync();

                    try
                    {
                        chunkContents = await new HttpClient().GetByteArrayAsync(file.GetChunkUrl(chunkIndex));
                    }
                    finally
                    {
                        _concurrentDownloadSemaphore.Release();
                    }

                    lock (_writeLock)
                        destination.Write(chunkContents, 0, chunkContents.Length);
                }));
            }

            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 2
0
        private static void DownloadFileVersion4(DownloadableFile file, FileStream destination)
        {
            var tasks = new List<Task>();

            foreach (int i in Enumerable.Range(0, file.ChunkCount))
            {
                int chunkIndex = i;

                tasks.Add(Task.Run(async delegate
                {
                    byte[] chunkContents;

                    using (await SemaphoreLock.TakeAsync(_concurrentDownloadSemaphore))
                        chunkContents = await new HttpClient().GetByteArrayAsync(file.GetChunkUrl(chunkIndex));

                    using (await SemaphoreLock.TakeAsync(_fileWriteSemaphore))
                        destination.Write(chunkContents, 0, chunkContents.Length);
                }));
            }

            Task.WaitAll(tasks.ToArray());
        }