In wpf application - UI getting freezed on button click - then how to show busy indicator while huge data processing?
I have tried background worker process, but its throwing the below exception.
The "calling thread" in the message is not your UI thread...
Sample Code:
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
BackgroundWorker worker = sender as BackgroundWorker;
this.busyIndicator.Visibility = Visibility.Visible;
busyIndicator.IsBusy = true;
for (int k = 1; (k <= 10); k++)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
int intAutomationID = 0;
int intAutomation_SS_ID = 0;
int intAS_ID = 0;
string strProcedureName = "";
//busyIndicator.Visibility = Visibility.Visible;
try
{
// Insert entry into AUTOMATION_PROCESS table.
intAutomationID = Pkg_TargetsIdentifiers.InsertAutomationProcess(Convert.ToInt32(cmbIdentifier.SelectedValue),
Convert.ToInt32(cmbSourceData.SelectedValue), "InProgress", 0, "Insert");
if (intAutomationID > 0)
{
for (int i = 0; i <= dgvProcessLists.Items.Count - 1; i++)
{
int j = 3;
strProcedureName = "";
strProcedureName = (dgvProcessLists.Items[i] as DataRowView).Row.ItemArray[j].ToString();
if (!string.IsNullOrEmpty(strProcedureName))
{
//AS_ID
// InitializeMouseHandlersForVisual(dgvProcessLists);
intAS_ID = Convert.ToInt32((dgvProcessLists.Items[i] as DataRowView).Row.ItemArray[0].ToString());
intAutomation_SS_ID = Pkg_TargetsIdentifiers.InsertAutomationStepsStatus(intAS_ID, intAutomationID,
"Inprogress", 0, "Insert");
bool boolStatus = Pkg_TargetsIdentifiers.CallActionProcess(strProcedureName, intAutomationID);
if (boolStatus == true)
{
//var selectedRow = DataProcessing.Class1.GetSelectedRow(this.dgvProcessLists);
//var columnCell = DataProcessing.Class1.GetRow(this.dgvProcessLists,0);
intAutomation_SS_ID = Pkg_TargetsIdentifiers.InsertAutomationStepsStatus(intAS_ID, intAutomationID,
"Completed", intAutomation_SS_ID, "Update");
intAS_ID = 0;
strProcedureName = "";
DataRowView row = (dgvProcessLists.Items[i] as DataRowView);
if (row != null)
{
if (row.DataView.Table.Columns.Contains("Status"))
{
Type type = row["Status"].GetType();
string status = row["Status"] == System.DBNull.Value ? null : (string)row["Status"];
if (boolStatus == true)
{
Uri uri = new Uri("pack://application:,,,/Images/green.jpg");
BitmapImage source = new BitmapImage(uri);
}
if (boolStatus == false)
{
Uri uri = new Uri("pack://application:,,,/Images/red.jpg");
BitmapImage source = new BitmapImage(uri);
}
}
}
continue;
}
else
{
break;
}
}
}
intAutomationID = Pkg_TargetsIdentifiers.InsertAutomationProcess(Convert.ToInt32(cmbIdentifier.SelectedValue),
Convert.ToInt32(cmbSourceData.SelectedValue), "Completed", intAutomationID, "Update");
}
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress((k * 10));
}
catch (Exception ex)
{
throw ex;
}
finally
{ }
}
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
if ((e.Cancelled == true))
{
this.busyIndicator.Visibility = Visibility.Hidden;
busyIndicator.IsBusy = false;
}
else if (!(e.Error == null))
{
this.busyIndicator.Visibility = Visibility.Hidden;
busyIndicator.IsBusy = false;
}
else
{
this.busyIndicator.Visibility = Visibility.Hidden;
busyIndicator.IsBusy = false;
}
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.busyIndicator.Visibility = Visibility.Visible;
busyIndicator.IsBusy = true;
}
/// <summary>
/// btnStartProcess_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStartProcess_Click(object sender, RoutedEventArgs e)
{
try
{
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
//worker.RunWorkerAsync();
// //};
// //worker.RunWorkerCompleted += (o, ea) =>
// //{
// // busyIndicator.Visibility = Visibility.Hidden;
// // busyIndicator.IsBusy = false;
// //};
// //worker.RunWorkerAsync();
// //};
// //worker.RunWorkerAsync();
//bw.RunWorkerCompleted += (o, ea) =>
//{
// busyIndicator.IsBusy = false;
// busyIndicator.Visibility = Visibility.Hidden;
//};
//busyIndicator.IsBusy = true;
//busyIndicator.Visibility = Visibility.Visible;
//bw.RunWorkerAsync();
}
catch (Exception ex)
{
throw ex;
}
}
Please do the needful..
Thanks and Regards, Vijay Babu