C#:如何在创建zip文件时报告进度?
更新:让它工作更新我的工作代码
这是我到目前为止所拥有的
private async void ZipIt(string src, string dest) { await Task.Run(() => { using (var zipFile = new ZipFile()) { // add content to zip here zipFile.AddDirectory(src); zipFile.SaveProgress += (o, args) => { var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d); // report your progress pbCurrentFile.Dispatcher.Invoke( System.Windows.Threading.DispatcherPriority.Normal, new Action( delegate() { pbCurrentFile.Value = percentage; } )); }; zipFile.Save(dest); } }); }
我需要弄清楚如何更新我的进度条,但不确定我是否在正确的轨道上我已经搜索过并找到了很多关于Windows窗体和vb.net的例子,但没有任何关于wpf c#想知道是否有人可以提供帮助。
我猜你在使用DotNetZip?
您展示的代码中存在许多问题:
解
改为使用任务和SaveProgress
事件:
private async void MainWindow_Loaded(object sender, RoutedEventArgs e) { await Task.Run(() => { using (var zipFile = new ZipFile()) { // add content to zip here zipFile.SaveProgress += (o, args) => { var percentage = (int) (1.0d/args.TotalBytesToTransfer*args.BytesTransferred*100.0d); // report your progress }; zipFile.Save(); } }); }
这样,您的用户界面就不会冻结,您将获得进度的定期报告。
总是喜欢BackgroundWorker
任务,因为它是现在使用的官方方法。
上述就是C#学习教程:C#:如何在创建zip文件时报告进度?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/959947.html