wpf C#:如何在创建 zip 文件时报告进度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22976841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:30:15  来源:igfitidea点击:

C# : How to report progress while creating a zip file?

c#wpfprogress-bardotnetzip

提问by ondrovic

Update: Got it working updated my working code

更新:让它工作更新了我的工作代码

Here is what I have so far

这是我到目前为止所拥有的

 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);
            }
        });
    }

I need to figure out how to update my progress bar but not sure if I am on the right track I have searched around and found many examples for windows forms and vb.net but nothing for wpf c# was wondering if anyone could help.

我需要弄清楚如何更新我的进度条,但不确定我是否在正确的轨道上我已经四处搜索并找到了许多关于 windows 窗体和 vb.net 的示例,但没有关于 wpf c# 想知道是否有人可以提供帮助。

回答by Aybe

I guess you are using DotNetZip ?

我猜你正在使用 DotNetZip ?

There are numerous issue in the code you've shown:

您显示的代码中有很多问题:

  • you don't call ReportProgressin DoWorkso how do you expect to get the progress ?
  • even if you did so the problem is with zip.Save()you wouldn't get a progress (beside 100%) because it would not return unless it is finished.
  • 你不叫ReportProgressDoWork你怎么这么希望得到的进展如何?
  • 即使你这样做了,问题是zip.Save()你不会得到进展(除了 100%),因为除非它完成,否则它不会返回。

Solution

解决方案

Use tasks and SaveProgressevent instead :

改用任务和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();
        }
    });
}

Doing this way, your UI will not freeze and you will get a periodic report of the progress.

这样做,您的 UI 不会冻结,您将定期获得进度报告。

Always prefer Tasks over BackgroundWorkersince it's officialapproach to use now.

总是更喜欢 Tasks,BackgroundWorker因为它是现在使用的官方方法。