処理中にローダーを表示するために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;
}
}
現在の問題とは関係ありませんが、 'throw exp;'は 'throw;'に変更する必要があります。 – mjwills
'pbStatus.Value = e.ProgressPercentage;'でブレークポイントを叩き、コードをデバッグしてください。何度ヒットしますか? – mjwills
@mjwills foreachループで "reportprogress"を呼び出すたびにヒットし、非同期ビヘイビアを示します。この行 "pbStatus.Value = e.ProgressPercentage"がヒットすると、このコードを含む関数はそのコードの実行を完了しませんループが終了するとその動作は完了します。このコード行の後に実際にスレッドを呼び出しスレッドに移動すると、それが更新されません。 –