인터넷에서 파일을 다운로드하는 간단한 콘솔 응용 프로그램을 빌드했습니다.
I had problems with WebClient 나는 HttpClient를 사용하여 내 응용 프로그램을 작성하기로 결정했습니다.진행률보고가있는 Stream.CopyToAsync - 복사 완료 후에도 진행률이보고됩니다.
기본적으로 헤더를 읽으라는 요청을하고 있습니다. ReadAsStreamAsync
CopyToAsync
을 사용하여 로컬 파일에 복사하는 스트림이 나타납니다.
나는 IProgress을 지원하는 스트림의 확장 방법을 발견했습니다
public static class StreamExtensions
{
public static async Task CopyToAsync(this Stream source, Stream destination, IProgress<long> progress, CancellationToken cancellationToken = default(CancellationToken), int bufferSize = 0x1000)
{
var buffer = new byte[bufferSize];
int bytesRead;
long totalRead = 0;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
totalRead += bytesRead;
//Thread.Sleep(10);
progress.Report(totalRead);
}
}
}
내 응용 프로그램이 작동을하지만 잘못된 진행 정보를 얻을 수 있습니다.
file1.tmp 60.95%
file2.tmp 98.09%
file1.tmp 60.98%
file2.tmp 98.21%
file2.tmp 98.17%
file2.tmp 98.25%
file1.tmp 61.02%
file2.tmp 98.41%
file2.tmp downloaded
file2.tmp 98.29%
file2.tmp 98.37%
file1.tmp 61.06%
file2.tmp 89.27%
file2.tmp 89.31%
file2.tmp 98.33%
file2.tmp 98.45%
file2.tmp 98.48%
file1.tmp 61.10%
file1.tmp 61.14%
file2.tmp 98.52%
file1.tmp 61.22%
file2.tmp 98.60%
file2.tmp 98.56%
file1.tmp 61.30%
file2.tmp 98.88%
file2.tmp 90.44%
file1.tmp 61.53%
file2.tmp 98.72%
file1.tmp 61.41%
file1.tmp 61.73%
file2.tmp 98.80%
file1.tmp 61.26%
file1.tmp 61.49%
file1.tmp 61.57%
file1.tmp 61.69%
...
file1.tmp 99.31%
file1.tmp 98.84%
file1.tmp 98.80%
file1.tmp 99.04%
file1.tmp 99.43%
file1.tmp 99.12%
file1.tmp 99.00%
file1.tmp downloaded
file1.tmp 100.00%
file1.tmp 98.73%
file1.tmp 98.88%
file1.tmp 99.47%
file1.tmp 99.98%
file1.tmp 99.90%
file1.tmp 98.96%
file1.tmp 99.78%
file1.tmp 99.99%
file1.tmp 99.74%
file1.tmp 99.59%
file1.tmp 99.94%
file1.tmp 98.49%
file1.tmp 98.53%
ALL FILES DOWNLOADED
file1.tmp 99.55%
file1.tmp 98.41%
file1.tmp 99.62%
file1.tmp 98.34%
file1.tmp 99.66%
file1.tmp 98.69%
file1.tmp 98.37%
당신은 내가 파일 2 다운로드되는 정보를 가지고 볼 수 있듯이,하지만 난 여전히 파일 1과 동일 CopyToAsync
에서 진행 보고서를 얻고있다 : 예를 들어
이 개 파일을 다운로드 나는 출력 창에이를 참조하십시오. 그 때문에
나는 가끔 이상한 콘솔 출력을 얻을 : 그 디버그 정보를 얻을 후
await streamToReadFrom.CopyToAsync(streamToWriteTo, progress, source.Token,0x2000);
Debug.WriteLine(filename+" downloaded");
:
가 이상적으로 내가 호출 할 때보다 확실하고 싶습니다를 진행률이보고되지 않습니다 (파일 다운로드). await
이 내 문제를 해결할 것이라고 생각했지만 그렇지 않습니다.
어떻게 해결할 수 있습니까? 임시 해결 방안으로 진행 상황을보고하기 직전에 Thread.Sleep을 CopyToAsync
에 추가합니다.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncDownloadTest
{
class Program
{
private const string LocalPath = @"D:\TEMP";
static void Main()
{
try
{
var filesToDownlad = new List<Tuple<string, string>>
{
new Tuple<string, string>("file1.tmp", "http://ipv4.download.thinkbroadband.com/10MB.zip"),
new Tuple<string, string>("file2.tmp", "http://ipv4.download.thinkbroadband.com/10MB.zip")
};
_consolePosition = -1;
Console.CursorVisible = false;
Parallel.ForEach(filesToDownlad, new ParallelOptions { MaxDegreeOfParallelism = 4 }, doc =>
{
DownloadFile(doc.Item2,doc.Item1).Wait();
});
Debug.WriteLine("ALL FILES DOWNLOADED");
Console.CursorVisible = true;
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
}
}
private static readonly object ConsoleLock = new object();
private static int _consolePosition;
static readonly CancellationTokenSource source = new CancellationTokenSource();
private static async Task DownloadFile(string url, string filename)
{
int currenctLineNumber = 0;
int currectProgress = 0;
try
{
lock (ConsoleLock)
{
_consolePosition++;
currenctLineNumber = _consolePosition;
}
long fileSize = -1;
IProgress<long> progress = new Progress<long>(value =>
{
decimal tmp = (decimal)(value * 100)/fileSize;
if (tmp != currectProgress && tmp > currectProgress)
{
lock (ConsoleLock)
{
currectProgress = (int)tmp;
Console.CursorTop = currenctLineNumber;
Console.CursorLeft = 0;
Console.Write("{0,10} - {2,11} - {1,6:N2}%", filename, tmp, "DOWNLOADING");
}
Debug.WriteLine("{1} {0:N2}%", tmp, filename);
}
});
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, source.Token))
{
response.EnsureSuccessStatusCode();
if (response.Content.Headers.ContentLength.HasValue) fileSize = response.Content.Headers.ContentLength.Value;
if (response.Content.Headers.ContentDisposition != null)
{
var tmp = response.Content.Headers.ContentDisposition.FileName.Replace("\"", "");
Debug.WriteLine("Real name: {0}",tmp);
}
using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
using (Stream streamToWriteTo = File.Open(Path.Combine(LocalPath, filename), FileMode.Create, FileAccess.Write))
{
await streamToReadFrom.CopyToAsync(streamToWriteTo, progress, source.Token,0x2000);
Debug.WriteLine(filename+" downloaded");
lock (ConsoleLock)
{
Console.CursorTop = currenctLineNumber;
Console.CursorLeft = 0;
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("{0,10} - {2,11} - {1,6:N2}%", filename, 100, "SUCCESS");
Console.ForegroundColor = oldColor;
}
}
}
}
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
lock (ConsoleLock)
{
Console.CursorTop = currenctLineNumber;
Console.CursorLeft = 0;
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,10} - {2,11} - {1,6:N2}%", filename, currectProgress, "ERROR");
Console.ForegroundColor = oldColor;
}
}
}
}
public static class StreamExtensions
{
public static async Task CopyToAsync(this Stream source, Stream destination, IProgress<long> progress, CancellationToken cancellationToken = default(CancellationToken), int bufferSize = 0x1000)
{
var buffer = new byte[bufferSize];
int bytesRead;
long totalRead = 0;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
totalRead += bytesRead;
Thread.Sleep(10);
progress.Report(totalRead);
}
}
}
}
나는 이것이 'async'를 사용하는 것과 아무런 관련이 없다고 생각하지만, 콘솔에 출력 할 때 코드가 잘못된 행/열을 설정하는 것과 관련이 있다고 생각합니다. '자물쇠'가있는 것이 잘못되었지만 더 이상 조사하지 않았습니다. – Krumelur
@Krumelur가 콘솔 관련 코드를 제거해도 여전히 VS 출력 창에서'file2.tmp downloaded'를 볼 수 있지만 그 후에도 계속 질문을합니다 (두 번째 코드 섹션 그 파일은'file1.tmp 60.95 %'로 표시됩니다.) – Misiu
'Parallel.ForEach'에서'DownloadFile (doc.Item2, doc.Item1) .Wait();'을주의 깊게 살펴보면,'Parallel' 클래스는 호출 스레드 작업자 스레드 중 하나 인 경우 호출하는 스레드에 'SynchronizationContext'가 있으면 프로그램을 교착 상태로 만듭니다. 'Parallel' 대신에 [TPL Dataflow] (https://msdn.microsoft.com/en-us/library/hh228603 (v = vs.110) .aspx)를보고 싶을 수도 있습니다. 비동기 함수가 전달되도록 지원하여'DownloadFile'을'.Wait()'를 호출하는 대신 직접 전달할 수 있습니다. –