标签文字下载c#后直接不更新了

问题描述 投票:0回答:1

我正在创建一个简单的应用程序,它可以将一个zip文件下载到一个给定的目录中,这个工作很好,我已经设法显示下载进度并更新一个进度条。我遇到的问题是,一旦下载完成,应用程序就会将压缩文件解压到指定的目的地,这一切都很好。我的问题是,我的标签没有直接改变它的文本,似乎要等到提取完成后才会改变。

应该发生的情况是:标签上写着 "下载文件",一旦下载完成,应该写着 "提取文件"

我认为问题出在Async下载方法上,但我不能确定。

我的代码。

using System;
using System.ComponentModel;
using System.Net;
using System.IO;
using Ionic.Zip;
using System.Diagnostics;
using System.Threading;

using MahApps.Metro.Controls;

namespace CloudBarsInstaller
{
    public partial class MainWindow : MetroWindow
    {
        WebClient client = new WebClient();

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadFileAsync(new Uri(@"C:\NEWBARS\NewInstaller\BarsStaging\Distributable.zip"), @"C:\Temp2\Distributable.zip");
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            var progress = e.ProgressPercentage;
            progressBar.Value = e.ProgressPercentage;
        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
           Stopwatch stopwatch = Stopwatch.StartNew();
            lblStatus.Content = "Extracting Bars Zip File";
            System.Threading.Thread.Sleep(2000);
            stopwatch.Stop();
            //once download is done extract the file

            var directoryPath = @"C:\Temp2\Distributable.zip";
           string extractPath = @"C:\Temp2\BarsInstaller";

            using (ZipFile zip = new ZipFile(directoryPath))
            {
                //zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

                zip.ExtractProgress += zipProgress;
                zip.ExtractAll(extractPath,ExtractExistingFileAction.OverwriteSilently);

            }
            //ZipFile.ExtractToDirectory(directoryPath, extractPath);
        }
        private void zipProgress(object sender, ExtractProgressEventArgs e)
        {
            if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
                this.progressBar.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);

            else if (e.EventType == ZipProgressEventType.Extracting_AfterExtractAll)
                this.progressBar.Value = 100;
        }
    } 
}
c# wpf mahapps.metro
1个回答
0
投票

我现在已经解决了这个问题,这个问题是因为线程锁定。 我修改了代码,在点击按钮时下载文件(这更容易),并更新标签,我使用了这个代码。

lblStatus.Invoke((Action)delegate
{

    lblStatus.Text = "Extracting Bars Zip File";

});
© www.soinside.com 2019 - 2024. All rights reserved.