且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将进度条添加到下面的进程

更新时间:2023-12-02 22:22:58



进度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.