本文整理汇总了C#中System.Progress.?.Report方法的典型用法代码示例。如果您正苦于以下问题:C# Progress.?.Report方法的具体用法?C# Progress.?.Report怎么用?C# Progress.?.Report使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Progress
的用法示例。
在下文中一共展示了Progress.?.Report方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FetchAsync
public async Task<TempPackage> FetchAsync(Package pkg, Progress progress)
{
var result = new TempPackage { WorkDirectory = new TempDirectory("winston") };
using (var handler = new HttpClientHandler { AllowAutoRedirect = true })
using (var client = NetUtils.HttpClient(handler))
using (var response = await client.GetAsync(pkg.Location, HttpCompletionOption.ResponseHeadersRead))
{
result.MimeType = response.Content.Headers.ContentType?.MediaType;
var uriFileName = pkg.Location.LastSegment();
// Heuristic: assume the last path segment is a file name if it contains a dot for the file extension
if (!uriFileName.Contains('.'))
{
uriFileName = null;
}
// Try to get the right file name to give hints about the file type to the extractor
result.FileName = response.Content.Headers.ContentDisposition?.FileName?.Trim('\"', '\\', '\'') ??
uriFileName ??
"package";
var total = response.Content.Headers.ContentLength ?? -1L;
using (var stream = await response.Content.ReadAsStreamAsync())
using (var output = File.Open(result.FullPath, FileMode.Create, FileAccess.ReadWrite))
{
var totalRead = 0L;
var buffer = new byte[81920]; // Default size from .NET docs on CopyTo
while (true)
{
var read = await stream.ReadAsync(buffer, 0, buffer.Length);
if (read == 0)
{
break;
}
await output.WriteAsync(buffer, 0, read);
totalRead += read;
// Can't report progress if there was no Content-Length
if (total > 0)
{
progress?.Report(totalRead * 1d / (total * 1d) * 100);
}
}
progress?.Report(100);
progress?.CompletedDownload();
}
}
var hash = await FileSystem.GetSha1Async(result.FullPath);
// Only check when Sha1 is specified in the package metadata
if (!string.IsNullOrWhiteSpace(pkg.Sha1) &&
!string.Equals(hash, pkg.Sha1, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException($"SHA1 hash of remote file {pkg.Location} did not match {pkg.Sha1}");
}
return result;
}