2014-11-08 4 views
-1

그래서 많은 시행 착오를 반복해서 시도해 보았습니다.이 작업을 수행 할 수없는 것 같습니다. 기본적으로 Ionic.DLL을 사용하여 zip 파일의 압축을 풀고 싶습니다. 당신도 볼 수 있듯이 http://dotnetzip.codeplex.com에서 여기에 관한 스레드를 만들었습니다 : Extract ZipFile Using C# With Progress Report 그래서 기본적으로 내가 뭘하는지 요약 해주세요. 3xbuttons : btnCancel, btnBrowse 및 btnStart, 1xbackground 노동자 : backgroundworker1 1xlabel : LABEL1 1xtextbox :을 textBox1 내가 가지고 내 윈폼을 원하는 무엇 1xprogressbarzipfile (Ionic.dll)을 사용하는 C# 배경 작업자

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

내가 가진 폼이 간단히 : 폴더를 찾은 다음 Start (btnStart)를 클릭하면 ProgressBar (timewise)에도 표시된 zip 추출 프로세스가 시작되고 cancel (btnCancel) 버튼은 압축 해제 프로세스를 취소합니다. 성공적으로 모든 작업을 수행했지만 그럴 수는 없습니다. 압축 해제 절차를 취소하는 방법 알아보기 그것은 실제로 .exe를 닫지 않으면 멈추지 않는 것처럼 보입니다. 나는 텍스트 박스와 브라우즈 버튼을 사용하지 않고 간단한 예제를 만들기로 결정했다. 그 예제는 이전에 묻은 질문 (위 링크)에 있지만, 백그라운드 작업자와 압축 해제 프로세스를 취소하는 방법을 구현하는 방법을 알지 못한다. 나는 현재 상황에 대한 진행 상황 보고서를 가질 수 있도록 배경 작업자를 분명히 사용할 필요가있다. 아무도 나의 예를 들어 주거나 ​​이것을하는 예제를 제공 할 수 있는가?

+0

예제를 수정 하시겠습니까? 이 질문과 함께 좋은 코드 예제를 게시해야합니다. http://stackoverflow.com/help/mcve를 참조하십시오. 그 동안 ProgressChanged 이벤트를 사용하고 있는지 확인하고'WorkerReportsProgress'를'true'로 설정하십시오. –

+0

ExtractProgressEventArgs에 대해 이미 알고 있지만 Cancel 속성이 있습니다. 따라서 단순히 e.Cancel = backgroundWorker1.CancellationPending보다 더 많은 것을 취하지는 않습니다. –

+0

예제를 사용하여 내 다른 예제에 연결하는 것으로 현재 예제를 추가했습니다. – Burgo855

답변

0

해결책을 찾았습니다. 저는 이오닉에서 벗어나 기존 참고 문헌 인 System.IO.Compression과 System.IO.Compression.FileSystem을 추가했습니다.