C#后台工作者与zipfile(Ionic.dll)

时间:2014-11-08 23:03:58

标签: c# multithreading winforms

所以我一直在尝试一遍又一遍的试验和错误,我似乎无法让它工作,基本上我想用http://dotnetzip.codeplex.com中的Ionic.DLL解压缩一个zip文件,你可以看到我也在这里提出了一个主题:Extract ZipFile Using C# With Progress Report所以基本上总结一下我的目标。

我有一张表格: 3xbuttons:btnCancel,btnBrowse和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();
            }
        }
    }
}

我希望winforms拥有的只是:浏览一个文件夹,然后单击Start(btnStart),开始拉链提取过程,该过程也显示在ProgressBar上(按时间),取消(btnCancel)按钮取消解压缩过程,我已成功完成所有事情,但我无法解决如何取消解压缩过程,它似乎永远不会停止,除非我实际关闭.exe下来。我决定在没有文本框和浏览按钮的情况下创建一个更简单的示例,该示例在我之前的问题(上面的链接)中,但我没有看到如何实现取消与后台工作程序的解压缩过程的方法,以及我肯定需要使用后台工作人员,所以我可以得到关于当前情况的进度报告等。任何人都可以调整我的例子或提供一个这样做的例子吗?

1 个答案:

答案 0 :(得分:0)

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