Backgroundworker won't report progress

前端 未结 6 983
情书的邮戳
情书的邮戳 2020-12-01 09:21

I have a background worker running a long database task. i want to show the progress bar while the task is running. Somehow the background worker won\'t report the progres

6条回答
  •  温柔的废话
    2020-12-01 10:11

    You need to break your DoWork method down into reportable progress and then call ReportProgress.

    Take for example the following:

    private void Something_DoWork(object sender, DoWorkEventArgs e) 
    {
        // If possible, establish how much there is to do
        int totalSteps = EstablishWorkload();
    
        for ( int i=0; i

    If your work can't be predetermined, try adding your own percentages:

    private void Something_DoWork(object sender, DoWorkEventArgs e) 
    {
        // some work
    
        (sender as BackgroundWorker).ReportProgress(25, null);
    
        // some work
    
        (sender as BackgroundWorker).ReportProgress(50, null);
    
        // some work
    
        (sender as BackgroundWorker).ReportProgress(60, null);
    
        // some work
    
        (sender as BackgroundWorker).ReportProgress(99, null);
    }
    

提交回复
热议问题