2017-08-22 11 views
-1

処理中にローダーを表示するためにBackgroundworkerを使用しています。ローダー(Progressbar)は更新されず、完了後にのみ更新されます。これは、ProgressChangedイベントの非同期動作が原因である可能性があります。BackgroundWorker.ReportProgress()プロパティが更新されていません

void worker_DoWork(object sender, DoWorkEventArgs e) 
{ 


     bool isCompletedSuccessfully = true; 
     FileSQLHelper objSqlHelper = new FileSQLHelper(); 
     PGPUtil utilityObj = new PGPUtil(); 


      #region Outcoming 
      try 
      { 

       objSqlHelper.OpenConnection(); 
       List<File> fileList = objSqlHelper.GetFilesList(); 
       double totalRecord = fileList.Count(); 
       int counter = 1; 

       foreach (var Fileitem in fileList) 
       { 

        double progressStatus = (counter/totalRecord) * 100; 
        (sender as BackgroundWorker).ReportProgress((int)progressStatus); 

        // my code here 

        counter++; 
       } 

      } 
      catch (Exception ex) 
      { 
       isCompletedSuccessfully = false; 
       MessageBox.Show(ex.Message, "Error"); 

      } 
      finally 
      { 
       if (isCompletedSuccessfully == true) 
       { 
        MessageBox.Show("Completed Successfully!", "Information"); 
       } 
       else if (isCompletedSuccessfully == false) 
       { 
        // MessageBox.Show("Completed Unsuccessfully!", "Information"); 
       } 

      } 
      #endregion 



} 

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     BackgroundWorker worker = new BackgroundWorker(); 
     worker.WorkerReportsProgress = true; 
     worker.DoWork += worker_DoWork; 
     worker.ProgressChanged += worker_ProgressChanged; 
     worker.RunWorkerAsync(); 
     // worker.RunWorkerAsync(); 

    } 

我々は進歩

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 

     try 
     { 
      pbStatus.Value = e.ProgressPercentage; 

     } 
     catch(Exception exp) 
     { 
      throw exp; 
     }  
    } 
+0

現在の問題とは関係ありませんが、 'throw exp;'は 'throw;'に変更する必要があります。 – mjwills

+0

'pbStatus.Value = e.ProgressPercentage;'でブレークポイントを叩き、コードをデバッグしてください。何度ヒットしますか? – mjwills

+0

@mjwills foreachループで "reportprogress"を呼び出すたびにヒットし、非同期ビヘイビアを示します。この行 "pbStatus.Value = e.ProgressPercentage"がヒットすると、このコードを含む関数はそのコードの実行を完了しませんループが終了するとその動作は完了します。このコード行の後に実際にスレッドを呼び出しスレッドに移動すると、それが更新されません。 –

答えて

-2

を報告したときに、私はあなたがProgressBarコントロールにProgressPercentage値を代入された直後に何かを導入する必要があると思い呼び出される関数、何Application.DoEventsに似たもの()はWinFormsでやっていました。 WPFでは、Dispatcherで同じことができます。それがどのように機能するかを見てください。これで解決策を理解して実装するには時間がかかることがあります。だけのSystem.Windows.Forms.dllをインポートした後、あなただけ速く上で移動する場合、あなたはまだここApplication.DoEvents()を使用することができ、しかしMSDN-

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] 
public void DoEvents() 
{ 
    DispatcherFrame frame = new DispatcherFrame(); 
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, 
     new DispatcherOperationCallback(ExitFrame), frame); 
    Dispatcher.PushFrame(frame); 
} 

public object ExitFrame(object f) 
{ 
    ((DispatcherFrame)f).Continue = false; 

    return null; 
} 

からこのコードサンプルを参照してください。あなたのWPFプロジェクト(これは一種の醜い解決策です)。

+0

誰にも 'Application.DoEvents()'を呼び出さないようにしてください。それは常に怖いアドバイスです。 'BackgroundWorker'の全体的なポイントが' Application.DoEvents() 'のように愚かなハックをするのを避けるのであれば、' BackgroundWorker'に関する質問の文脈では二重です。 –

関連する問題