简体   繁体   English

BackgroundWorker中的DotNetZip更新进度栏和表单上的标签

[英]DotNetZip in BackgroundWorker update progressbar and label on a form

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. 我正在创建使用DotNetZip备份我的应用程序数据的选项,并避免冻结该应用程序,我发现这种类型的最佳操作的最佳方法是使用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;
    }

and this is BackupParams 这是BackupParams

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

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 我知道我可以使用ZipFile中的那些方法

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

but from those I don't have access progressbar or label that are on form... 但是从那些我没有访问表单上的进度条或标签的人中...

For sending a progress report out from a BackgroundWorker you use ReportProgress() inside your DoWork method. 要从BackgroundWorker发送进度报告,请在DoWork方法中使用ReportProgress()

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

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

This then fires off your worker_ProgressChanged method, so you can take the report from there into your controls. 然后,这将触发您的worker_ProgressChanged方法,因此您可以从那里将报告导入控件。

The trick is that you have to make another function to handle the progress change with your zip creation. 诀窍是您必须创建另一个函数来处理zip创建过程中的进度更改。 You can't access your UI controls here because they are on a different thread. 您无法在此处访问您的UI控件,因为它们位于不同的线程上。 You should be able to create a lambda for this (and I don't know the exact parameters, please fix if I'm wrong) 您应该可以为此创建一个lambda(而且我不知道确切的参数,如果我错了,请修复)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM