Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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 ?


C#
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);


         }
Posted
Updated 24-Jun-14 21:45pm
v3
Comments
Sergey Alexandrovich Kryukov 25-Jun-14 2:53am    
Progress bars are not designed to be added to a method. To start with, please tag your UI library you want to use to create your UI.
Or, talking about "ProgressBar", clearly indicate which one. Full type name, please.
—SA

1 solution


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.



The simplest way is to execute UnZip as a BackgroundWorker[^].



  • Declare UnZip as a BackgroundWorker
    C#
    private System.ComponentModel.BackgroundWorker UnZip_BW;

    as a class variable.
  • In the constructor, attach event handlers to UnZip_BW
    C#
    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
    C#
    UnZip_BW.RunWorkerCompleted += 
        new RunWorkerCompletedEventHandler ( UnZip_RunWorkerCompleted );

    and declare the UnZip_RunWorkerCompleted event handler as
    C#
    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();
            }
        }

    where result_LAB is declared in the Form.
  • Declare the UnZip_DoWork method:
    C#
    private void UnZip_DoWork ( object          sender, 
                                DoWorkEventArgs e )
        {   
        BackgroundWorker worker = ( BackgroundWorker ) sender;
    
        :
        :
        }

  • 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.
    C#
    private void UnZip_ProgressChanged ( object                   sender,
                                         ProgressChangedEventArgs e )
        {
        this.progressBar1.Value = e.ProgressPercentage;
        }

    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.
    C#
    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
    C#
    UnZip_BW.RunWorkerAsync ( );



Hope that helps.

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900