BackgroundWorker中的DotNetZip更新进度栏和表单上的标签 [英] DotNetZip in BackgroundWorker update progressbar and label on a form

查看:126
本文介绍了BackgroundWorker中的DotNetZip更新进度栏和表单上的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建使用DotNetZip备份我的应用程序数据的选项,并避免冻结该应用程序,我发现这种最佳操作方式的最佳方法是使用BackgroundWorker.所以我想到了这样的东西:

I'm creating option to backup data of my app with DotNetZip and to avoid freezing the app I've found that the best way for a this type of action best way is to use BackgroundWorker. So I came with something like this:

    private void processButton_Click(object sender, EventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

        BackupParams bp = new BackupParams();
        bp.Source = inputTextBox.Text;  // source dir
        bp.Output = outputTextBox.Text; // output file
        bp.Password = @"Pa$$w0rd";

        worker.RunWorkerAsync(bp);
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show((string)e.Result, "Zip", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackupParams bp = (BackupParams)e.Argument;

        string id = Guid.NewGuid().ToString();
        comment += "Created at: " + DateTime.Now.ToString() + "\n";
        comment += id;

        ZipFile zf = new ZipFile();
        zf.Comment = comment;
        zf.CompressionMethod = CompressionMethod.BZip2;
        zf.CompressionLevel = CompressionLevel.BestCompression;

        zf.Encryption = EncryptionAlgorithm.WinZipAes256;
        zf.Password = bp.Password;

        zf.Name = bp.Output;

        zf.AddDirectory(bp.Source);

        zf.Save();

        e.Result = bp.Output;
    }

这是BackupParams

and this is BackupParams

public class BackupParams
{
    public string Source { get; set; }
    public string Output { get; set; }
    public string Password { get; set; }
}

现在我被卡住了,因为我想显示添加到存档中的文件的进度(名称百分比).做这个的最好方式是什么?我知道我可以使用ZipFile中的那些方法

And right now I'm stuck cause I want to show the progress (percentage with names) of files added to the archive. What is the best way to do this? I know i can use those methods from ZipFile

zf.SaveProgress += zf_SaveProgress;
zf.AddProgress += zf_AddProgress;

但是对于那些我没有访问表单上的进度条或标签的人...

but from those I don't have access progressbar or label that are on form...

推荐答案

要从BackgroundWorker发送进度报告,请在DoWork方法内使用ReportProgress().

For sending a progress report out from a BackgroundWorker you use ReportProgress() inside your DoWork method.

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker theWorker = (BackgroundWorker)sender;
    theWorker.ReportProgress(0, "just starting");

    BackupParams bp = (BackupParams)e.Argument;
    ...

这将触发您的worker_ProgressChanged方法,因此您可以从此处将报告放入控件中.

This then fires off your worker_ProgressChanged method, so you can take the report from there into your controls.

诀窍在于,您必须创建另一个函数来处理zip创建过程中的进度更改.您无法在此处访问您的UI控件,因为它们位于不同的线程上.您应该可以为此创建一个lambda(而且我不知道确切的参数,如果我输入错了,请修复)

The trick is that you have to make another function to handle the progress change with your zip creation. You can't access your UI controls here because they are on a different thread. You should be able to create a lambda for this (and I don't know the exact parameters, please fix if I'm wrong)

zf.SaveProgress += (sender, eventArgs) => 
{
    // Check if EvenType equals Saving_AfterWriteEntry or NullReferenceException will be thrown
    if (eventArgs.EventType == ProgressEventType.Saving_AfterWriteEntry)
    {
        theWorker.ReportProgress(eventArgs.EntriesSaved, "Saving "+ eventArgs.CurrentEntry.FileName);
    }
};

zf.AddProgress += (sender, eventArgs) => 
{
    // Check if EventType equals Adding_afterAddEntry or NullReferenceException will be thrown
    if (eventArgs.EventType == ZipProgressEventType.Adding_afterAddEntry)
    {
        theWorker.ReportProgress(eventArgs.EntriesAdded, "Adding "+ eventArgs.CurrentEntry.FileName);
    }
};

这篇关于BackgroundWorker中的DotNetZip更新进度栏和表单上的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆