当前位置: 首页>>代码示例>>C#>>正文


C# Progress.?.Report方法代码示例

本文整理汇总了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;
        }
开发者ID:mattolenik,项目名称:winston,代码行数:56,代码来源:HttpFetcher.cs


注:本文中的System.Progress.?.Report方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。