コード例 #1
0
ファイル: Program.cs プロジェクト: jsnape/abide-nhs
        /// <summary>
        /// Throttles the downloads.
        /// </summary>
        /// <param name="source">Source file.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
        /// <returns>A task representing the completion of this function.</returns>
        private static async Task Throttle(SourceFile source, CancellationToken cancellationToken)
        {
            Console.WriteLine("Waiting to download {0}", source.FileName);

            await Throttler.WaitAsync();
            
            try
            {
                await DownloadFile(source, cancellationToken);
            }
            finally
            {
                Throttler.Release();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: jsnape/abide-nhs
        /// <summary>
        /// Downloads a single file.
        /// </summary>
        /// <param name="source">Source file.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
        /// <returns>A task representing the completion of this function.</returns>
        private static async Task DownloadFile(SourceFile source, CancellationToken cancellationToken)
        {
            var path = Path.Combine(Program.Options.DownloadPath, source.FileName);

            Console.WriteLine("Downloading {0}", path);

            using (var client = new HttpClient())
            {
                using (var output = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var stream = await client.GetStreamAsync(source.Uri))
                {
                    await stream.CopyToAsync(output);
                }
            }

            Console.WriteLine("Completed {0}", path);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: jsnape/abide-nhs
 /// <summary>
 /// Retries the downloads.
 /// </summary>
 /// <param name="source">Source file.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
 /// <returns>A task representing the completion of this function.</returns>
 private static async Task Retry(SourceFile source, CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         try
         {
             await Throttle(source, cancellationToken);
             return;
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     }
 }