2014-11-08 35 views
-1

因此,我一直试图反复尝试和错误,我似乎无法得到这个工作,基本上我想解压缩使用Ionic.DLL文件从http://dotnetzip.codeplex.com,你可以看到我也在这里做了一个线程:Extract ZipFile Using C# With Progress Report所以基本上总结了我后。C#背景工人与zipfile(Ionic.dll)

我有一个形式: 3xbuttons:btnCancel,btnBrowse和btnStart, 1xbackground工人:backgroundworker1 1xlabel:LABEL1 1xtextbox:textBox1的 1xprogressbar

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Threading; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using Ionic.Zip; 

namespace BackgroundWorkerSample 
{ 
    public partial class Form1 : Form 
    { 
     /// <summary> 
     /// The backgroundworker object on which the time consuming operation shall be executed 
     /// </summary> 
     BackgroundWorker m_oWorker; 

     public Form1() 
     { 
      InitializeComponent(); 
      m_oWorker = new BackgroundWorker(); 
      m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork); 
      m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged); 
      m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted); 
      m_oWorker.WorkerReportsProgress = true; 
      m_oWorker.WorkerSupportsCancellation = true; 
     } 

     /// <summary> 
     /// On completed do the appropriate task 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      //If it was cancelled midway 
      if (e.Cancelled) 
      { 
       lblStatus.Text = "Task Cancelled."; 
      } 
      else if (e.Error != null) 
      { 
       lblStatus.Text = "Error while performing background operation."; 
      } 
      else 
      { 
       lblStatus.Text = "Task Completed..."; 
      } 
      btnStartAsyncOperation.Enabled = true; 
      btnCancel.Enabled = false; 
     } 

     /// <summary> 
     /// Notification is performed here to the progress bar 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      //Here you play with the main UI thread 
      progressBar1.Value = e.ProgressPercentage; 
      lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%"; 
     } 

     /// <summary> 
     /// Time consuming operations go here </br> 
     /// i.e. Database operations,Reporting 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     void m_oWorker_DoWork(object sender, DoWorkEventArgs e) 
     { 
      //NOTE : Never play with the UI thread here... 

      //time consuming operation 
      for (int i = 0; i < 100; i++) 
      { 
       Thread.Sleep(100); 
       m_oWorker.ReportProgress(i); 



       /////////////////////MINEEEEEEEEEEEEEEEEEEEEEEEEE 
       string INSTALL_LOCATION = "C:" + @"\" + "Program Files (x86)" + @"\" + "TEST_FOLDER" + @"\" + @"\" + "Burgos_Folder"; 
       string DEFAULT_LOCATION = "C:" + @"\" + "Program Files (x86)" + @"\" + "TEST_FOLDER" + @"\" + "test.rar"; 

       if (!Directory.Exists(INSTALL_LOCATION)) 
       { 
        Directory.CreateDirectory(INSTALL_LOCATION); 
       } 

       //if the textbox directory exists 
       if (Directory.Exists(INSTALL_LOCATION)) 
       { 
        using (ZipFile zip = ZipFile.Read(DEFAULT_LOCATION)) 
          { 
           zip.ExtractAll(INSTALL_LOCATION, ExtractExistingFileAction.OverwriteSilently); 
          } 
       } 

       //If cancel button was pressed while the execution is in progress 
       //Change the state from cancellation ---> cancel'ed 
       if (m_oWorker.CancellationPending) 
       { 
        e.Cancel = true; 
        m_oWorker.ReportProgress(0); 
        return; 
       } 

      } 

      //Report 100% completion on operation completed 
      m_oWorker.ReportProgress(100); 
     } 

     private void btnStartAsyncOperation_Click(object sender, EventArgs e) 
     { 
      btnStartAsyncOperation.Enabled = false; 
      btnCancel.Enabled    = true; 
      //Start the async operation here 
      m_oWorker.RunWorkerAsync(); 
     } 

     private void btnCancel_Click(object sender, EventArgs e) 
     { 
      if (m_oWorker.IsBusy) 
      { 
       //Stop/Cancel the async operation here 
       m_oWorker.CancelAsync(); 
      } 
     } 
    } 
} 

什么,我想我的WinForms已经是简单地:浏览一个文件夹,然后点击开始(btnStart),它将启动压缩提取过程,该过程也显示在ProgressBar上(按时间显示),取消(btnCancel)按钮取消解压缩过程,我已成功完成所有操作,但不能弄清楚如何取消解压缩过程ss,它永远不会停止,除非我真的关闭.exe。我决定做一个没有文本框和浏览按钮的简单例子,这个例子在我之前提到的问题(上面的链接)中,但我没有看到如何实现一种方法来取消与后台工作人员的解压缩过程,以及我绝对需要使用后台工作,所以我可以有一个关于当前情况的进度报告等任何人都可以请适应我的例子或提供一个这样做的例子吗?

+0

适应什么样的例子?你需要发布一个很好的代码示例,这个问题。请参阅http://stackoverflow.com/help/mcve。同时,确保您使用'ProgressChanged'事件并将'WorkerReportsProgress'设置为'true'。 – 2014-11-08 23:19:16

+0

您已经了解ExtractProgressEventArgs,它具有Cancel属性。所以它不仅仅是简单的e.Cancel = backgroundWorker1.CancellationPending; – 2014-11-08 23:33:56

+0

我已经添加了我目前的示例球员,我想通过示例链接到我的其他问题就足够了。 – Burgo855 2014-11-08 23:50:22

回答

0

我发现了一个解决方案,我已经离开了Ionic并添加了现有的引用:System.IO.Compression和System.IO.Compression.FileSystem。