我在解压缩文件时遇到了一些问题。 一切正常进展条输出和提取。 但是当它运行时,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)); }); }更多推荐
发布评论