So i have been trying over and over with lots of trial and error and i cannot seem to get this to work, basically i want to unzip a zip file using the Ionic.DLL from http://dotnetzip.codeplex.com as you can see i have also made a thread about it here: Extract ZipFile Using C# With Progress Report so to basically sum up what i am after.
I have a form with: 3xbuttons: btnCancel, btnBrowse and btnStart, 1xbackground worker: 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();
}
}
}
}
What i would like my winforms to have is simply: browse for a folder, then click Start(btnStart) which starts the zip extraction process which is also shown on the ProgressBar(timewise), and the cancel(btnCancel) button cancels the unzipping processs, i have successfully done everything but i cannot work out how to cancel the unzipping process, it never seems to stop unless i actually close the .exe down. I decided to make a simpler example without the textbox and browse button, and that example is in my previous Asked Question(link above), but i don't see how to implement a way to cancel the unzipping process with the background worker, and i definantly need to use background worker so i can have a progress report on the current situation etc. Can anyone please adapt my examples or provide a example that does this?