C#具有zipfile(Ionic.dll)的后台工作程序
所以我反复尝试了很多尝试和错误,但我似乎无法实现这一点,基本上我想使用Ionic.DLL解压一个zip文件,正如你所看到的,我在这里也做了一个线程:因此,基本上总结一下我所追求的 我有一张表格,上面写着: 3个按钮:btnCancel、btnBrowse和btnStart, 1xB后台工作人员:后台工作人员1 1xlabel:label1 1xtextbox:textBox1 1XpressBarC#具有zipfile(Ionic.dll)的后台工作程序,c#,multithreading,winforms,C#,Multithreading,Winforms,所以我反复尝试了很多尝试和错误,但我似乎无法实现这一点,基本上我想使用Ionic.DLL解压一个zip文件,正如你所看到的,我在这里也做了一个线程:因此,基本上总结一下我所追求的 我有一张表格,上面写着: 3个按钮:btnCancel、btnBrowse和btnStart, 1xB后台工作人员:后台工作人员1 1xlabel:label1 1xtextbox:textBox1 1XpressBar using System; using System.Collections.Generic; u
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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统线程;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
使用Ionic.Zip;
命名空间BackgroundWorkerSample
{
公共部分类Form1:Form
{
///
///应在其上执行耗时操作的backgroundworker对象
///
背景工人m_Owworker;
公共表格1()
{
初始化组件();
m_owworker=新的BackgroundWorker();
m_oWorker.DoWork+=新的doworkerventhandler(m_oWorker\u DoWork);
m_oWorker.ProgressChanged+=新的ProgressChangedEventHandler(m_oWorker\u ProgressChanged);
m_oWorker.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(m_oWorker\u RunWorkerCompleted);
m_oWorker.WorkerReportsProgress=真;
m_owworker.worker支持扫描单元=真;
}
///
///完成后,执行适当的任务
///
///
///
void m_owworker_RunWorkerCompleted(对象发送方,runworkercompletedeventarge)
{
//如果中途取消的话
如果(如已取消)
{
lblStatus.Text=“任务已取消。”;
}
否则如果(例如错误!=null)
{
lblStatus.Text=“执行后台操作时出错。”;
}
其他的
{
lblStatus.Text=“任务已完成…”;
}
btnStartAsyncOperation.Enabled=true;
btnCancel.Enabled=false;
}
///
///在此处向进度条执行通知
///
///
///
void m_oWorker_ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
//在这里,您可以使用主UI线程
progressBar1.值=e.ProgressPercentage;
lblStatus.Text=“处理……”+progressBar1.Value.ToString()+“%”;
}
///
///耗时的操作在这里进行
///即数据库操作、报告
///
///
///
void m_oWorker_DoWork(对象发送方,doworkereventargs e)
{
//注意:永远不要在这里玩UI线程。。。
//耗时操作
对于(int i=0;i<100;i++)
{
睡眠(100);
m_oWorker.报告进度(i);
/////////////////////我的
string INSTALL\u LOCATION=“C:”+@“\”+“程序文件(x86)”+@“\”+“测试”文件夹“+@“\”+”Burgos\u文件夹”;
字符串DEFAULT\u LOCATION=“C:”++“\”+”程序文件(x86)”++“\”+”测试文件夹“++“\”+”测试.rar”;
如果(!Directory.Exists(安装位置))
{
CreateDirectory(安装位置);
}
//如果文本框目录存在
if(目录.Exists(安装位置))
{
使用(ZipFile zip=ZipFile.Read(默认位置))
{
ExtractAll(安装位置,ExtractExistingFileAction.OverwriteSilently);
}
}
//如果在执行过程中按下“取消”按钮
//从取消更改状态-->cancel'ed
if(m_oWorker.CancellationPending)
{
e、 取消=真;
m_oWorker.ReportProgress(0);
返回;
}
}
//报告操作完成后100%完成
m_oWorker.报告进度(100);
}
私有void btnStartAsyncOperation\u单击(对象发送方,事件参数e)
{
btnStartAsyncOperation.Enabled=false;
btnCancel.Enabled=真;
//在这里启动异步操作
m_owworker.RunWorkerAsync();
}
私有void btnCancel\u单击(对象发送者,事件参数e)
{
如果(m_oWorker.IsBusy)
{
//在此停止/取消异步操作
m_oWorker.CancelAsync();
}
}
}
}
我想让我的winforms拥有的只是:浏览文件夹,然后单击开始(btnStart),开始zip提取过程,该过程也显示在进度栏上(按时间),取消(btnCancel)按钮取消解压缩过程,我已经成功地完成了所有操作,但我无法确定如何取消解压缩过程,它似乎永远不会停止,除非我真的关闭了.exe。我决定制作一个没有文本框和浏览按钮的简单示例,该示例在我之前提出的问题(上面的链接)中,但我不知道如何实现一种方法来取消后台工作程序的解压缩过程,我确实需要使用后台工作人员,这样我就可以有一份关于当前情况的进度报告等。是否有人可以修改我的示例或提供一个这样做的示例?我找到了一个解决方案,我已经离开了Ionic,添加了现有的参考:System.IO.Compression和System.IO.Compression.FileSystem。修改哪些示例?您需要发布一个好的代码示例,并附带此问题。看见在