如何将进度条添加到下面的进程 [英] How to add progress bar to below process

查看:87
本文介绍了如何将进度条添加到下面的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码我有提取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;



作为类变量。

  • 在构造函数中,将事件处理程序附加到UnZip_BW


    as a class variable.

  • In the constructor, attach event handlers to UnZip_BW

    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在表格中声明。

  • 声明UnZip_DoWork方法:


    where result_LAB is declared in the Form.

  • Declare the UnZip_DoWork method:

    private void UnZip_DoWork ( object          sender, 
                                DoWorkEventArgs e )
        {   
        BackgroundWorker worker = ( BackgroundWorker ) sender;
    
        :
        :
        }



  • 将UnZip方法内容放入UnZip_DoWork方法。

  • 声明UnZip_ProgressChanged方法。它已作为UnZip_ProgressChanged事件的事件处理程序连接。


  • Place your UnZip method contents into the UnZip_DoWork method.
  • Declare the UnZip_ProgressChanged method. It is already wired as the event handler for the UnZip_ProgressChanged event.

    private void UnZip_ProgressChanged ( object                   sender,
                                         ProgressChangedEventArgs e )
        {
        this.progressBar1.Value = e.ProgressPercentage;
        }



    progressBar1假定已在您的表格中声明。

  • 这是困难的部分鉴于您提供的代码。在UnZip_DoWork方法的某个位置,当要报告进度时,计算已完成工作的百分比(percent_completed)并引发ReportProgress事件。


    progressBar1 is assumed to have been declared in your Form.

  • Here's the hard part given the code you have provided. Somewhere in the UnZip_DoWork method, when progress is to be reported, compute the percentage of work completed (percent_completed) and raise the ReportProgress event.

    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.

  • In the constructor, after you've attached the event handlers, add the code

    UnZip_BW.RunWorkerAsync ( );





  • 希望有所帮助。


    Hope that helps.


    这篇关于如何将进度条添加到下面的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆