0

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

    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

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

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

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

但从那些我无法访问表单上的进度条或标签...

4

1 回答 1

1

ReportProgress()为了从您在DoWork方法中使用的 BackgroundWorker 发送进度报告。

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

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

然后这会触发您的worker_ProgressChanged方法,因此您可以将报告从那里带到您的控件中。

诀窍是您必须创建另一个函数来处理 zip 创建的进度更改。您无法在此处访问您的 UI 控件,因为它们位于不同的线程上。您应该能够为此创建一个 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);
    }
};
于 2013-10-17T00:32:17.153 回答