2013-10-17 75 views
0

我創建選項來備份DotNetZip應用程序的數據,並避免凍結應用程序我發現,這種類型的行動最好的方式的最佳途徑是使用BackgroundWorker。於是,我帶着這樣的: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; 

但那些我沒有說是對形式的訪問進度或標籤...

回答

1

對於自己使用的一個BackgroundWorker出發送進度報告ReportProgress()您的DoWork方法。

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

我不得不修改了一下那個lambda表達式來檢查'eventArgs.EventType '如果它是一個'ZipProgressEventType.Adding_afterAddEntry'和'ZipProgressEventType.Saving_AfterWriteEntry',或者它拋出'NullReferenceException' – Dawid

+0

@ gnur2171 done。現在這個代碼適用於我。我有保存進度和保存的文件名稱顯示。正是我在找什麼。 – Dawid