使用sevenzipsharp进行C#解压缩并更新进度栏,而无需UI冻结(C# extracting with sevenzipsharp and update progress bar withou

编程入门 行业动态 更新时间:2024-10-24 08:23:06
使用sevenzipsharp进行C#解压缩并更新进度栏,而无需UI冻结(C# extracting with sevenzipsharp and update progress bar without UI freeze)

我在解压缩文件时遇到了一些问题。 一切正常进展条输出和提取。 但是当它运行时,UI会冻结。 我试图使用Task.Run()但它不能很好地处理进度条。 或者,也许我只是没有正确使用它。

有什么建议么?

private void unzip(string path) { this.progressBar1.Minimum = 0; this.progressBar1.Maximum = 100; progressBar1.Value = 0; this.progressBar1.Visible = true; var sevenZipPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Environment.Is64BitProcess ? "x64" : "x86", "7z.dll"); SevenZipBase.SetLibraryPath(sevenZipPath); var file = new SevenZipExtractor(path + @"\temp.zip"); file.Extracting += (sender, args) => { this.progressBar1.Value = args.PercentDone; }; file.ExtractionFinished += (sender, args) => { // Do stuff when done }; //Extract the stuff file.ExtractArchive(path); }

I'm having some problems with the file extraction. Everything works well with the progress bar output and the extraction. But when it is running the UI freezes. I've tried to use Task.Run() but then it doesn't really work well with the progress bar. Or maybe I just didn't use it correctly.

Any suggestions?

private void unzip(string path) { this.progressBar1.Minimum = 0; this.progressBar1.Maximum = 100; progressBar1.Value = 0; this.progressBar1.Visible = true; var sevenZipPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Environment.Is64BitProcess ? "x64" : "x86", "7z.dll"); SevenZipBase.SetLibraryPath(sevenZipPath); var file = new SevenZipExtractor(path + @"\temp.zip"); file.Extracting += (sender, args) => { this.progressBar1.Value = args.PercentDone; }; file.ExtractionFinished += (sender, args) => { // Do stuff when done }; //Extract the stuff file.ExtractArchive(path); }

最满意答案

您可能希望查看.NET Framework中的Progress<T>对象 - 它简化了跨线程添加进度报告。 这是一篇很好的博客文章,比较了BackgroundWorker vs Task.Run() 。 看看他如何在Task.Run()示例中使用Progress<T> 。

更新 - 这是它可能如何查找您的示例。 我希望这会给你足够的理解,以便将来能够使用Progress<T>类型。 :d

private void unzip(string path) { progressBar1.Minimum = 0; progressBar1.Maximum = 100; progressBar1.Value = 0; progressBar1.Visible = true; var progressHandler = new Progress<byte>( percentDone => progressBar1.Value = percentDone); var progress = progressHandler as IProgress<byte>; Task.Run(() => { var sevenZipPath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Environment.Is64BitProcess ? "x64" : "x86", "7z.dll"); SevenZipBase.SetLibraryPath(sevenZipPath); var file = new SevenZipExtractor(path); file.Extracting += (sender, args) => { progress.Report(args.PercentDone); }; file.ExtractionFinished += (sender, args) => { // Do stuff when done }; //Extract the stuff file.ExtractArchive(Path.GetDirectoryName(path)); }); }

You may want to look into the Progress<T> object in the .NET Framework - it simplifies adding progress reporting across threads. Here is a good blog article comparing BackgroundWorker vs Task.Run(). Take a look at how he is using Progress<T> in the Task.Run() examples.

Update - Here is how it might look for your example. I hope this gives you a good enough understanding to be able to use the Progress<T> type in the future. :D

private void unzip(string path) { progressBar1.Minimum = 0; progressBar1.Maximum = 100; progressBar1.Value = 0; progressBar1.Visible = true; var progressHandler = new Progress<byte>( percentDone => progressBar1.Value = percentDone); var progress = progressHandler as IProgress<byte>; Task.Run(() => { var sevenZipPath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Environment.Is64BitProcess ? "x64" : "x86", "7z.dll"); SevenZipBase.SetLibraryPath(sevenZipPath); var file = new SevenZipExtractor(path); file.Extracting += (sender, args) => { progress.Report(args.PercentDone); }; file.ExtractionFinished += (sender, args) => { // Do stuff when done }; //Extract the stuff file.ExtractArchive(Path.GetDirectoryName(path)); }); }

更多推荐

本文发布于:2023-07-24 19:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解压缩   进度   UI   sevenzipsharp   extracting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!