如何将进度条添加到下面的进程 [英] How to add progress bar to below process
问题描述
在下面的代码我有提取rar文件的方法。我想以百分比显示提取进度。我可以这样做吗?
in the below code i have method to extract rar files.i want to show progress for extracting in percentage.how can i do that ?
public void UnZip()
{
string DestinationPath = @"C:\Temp\DATA_UNITS\BusinessObjectsServer_win\response.ini";
Shell32.Shell sc = new Shell32.Shell();
System.IO.Directory.CreateDirectory("C:\\TEMP");
Shell32.Folder output = sc.NameSpace("C:\\TEMP");
string sourcePath = Application.StartupPath + "\\51047839.ZIP";
Shell32.Folder input = sc.NameSpace(sourcePath);
output.CopyHere(input.Items());
System.IO.File.Delete(DestinationPath);
}
推荐答案
进度bar是一个UI组件。因此,它必须在主UI线程中执行。我假设您的UnZip方法非常耗时,并且您想要衡量其进度。
A progress bar is a UI component. As such it must execute in the main UI thread. I assume that your UnZip method is time consuming and that you want to measure its progress.
最简单的方法是将UnZip作为 BackgroundWorker 执行[ ^ ]。
The simplest way is to execute UnZip as a BackgroundWorker[^].
- 宣布UnZip为BackgroundWorker
- Declare UnZip as a BackgroundWorker
private System.ComponentModel.BackgroundWorker UnZip_BW;
作为类变量。
as a class variable.
UnZip_BW.DoWork += new DoWorkEventHandler ( UnZip_DoWork );
UnZip_BW.ProgressChanged +=
new ProgressChangedEventHandler ( UnZip_ProgressChanged );
你做的似乎不需要工人完成的事件处理程序,但如果你这样做,请使用
You do not appear to need a worker completed event handler, but if you do, use
UnZip_BW.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler ( UnZip_RunWorkerCompleted );
并将UnZip_RunWorkerCompleted事件处理程序声明为
and declare the UnZip_RunWorkerCompleted event handler as
private void UnZip_RunWorkerCompleted (
object sender,
RunWorkerCompletedEventArgs e )
{
if ( e.Error != null )
{
// handle exception
MessageBox.Show ( e.Error.Message );
}
else if ( e.Cancelled )
{
// report cancellation
result_LAB.Text = "Canceled";
}
else
{
// report success
result_LAB.Text = e.Result.ToString();
}
}
其中result_LAB在表格中声明。
where result_LAB is declared in the Form.
private void UnZip_DoWork ( object sender,
DoWorkEventArgs e )
{
BackgroundWorker worker = ( BackgroundWorker ) sender;
:
:
}
private void UnZip_ProgressChanged ( object sender,
ProgressChangedEventArgs e )
{
this.progressBar1.Value = e.ProgressPercentage;
}
progressBar1假定已在您的表格中声明。
progressBar1 is assumed to have been declared in your Form.
int percent_completed = 0;
:
:
worker.ReportProgress ( percent_completed );
问题是我在你提供的代码中没有看到钩子,你可以确定完成的百分比。 没有它,你就没有进度条。
The problem is that I do not see a "hook" in your supplied code where you can determine the percent completed. Without that, you cannot have a progress bar.
UnZip_BW.RunWorkerAsync ( );
希望有所帮助。
Hope that helps.
这篇关于如何将进度条添加到下面的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!