Reputation: 410
I managed to update a progress bar in real-time in WPF VSTO application using the below code
But, there is one problem, the line object[,] obj = GetOutput(objInput);
throws an error saying that it cannot access this object as it is in different thread. When I put this under Dispatch.Invoke then my Progress bar does not update in real-time. Can someone help me here
private BackgroundWorker backgroundWorker1 = new BackgroundWorker();
private object[,] GetOutput(object[,] obj)
{
object[,] objOutput = null;
if (rdUpper.IsChecked == true) //--- This UI element is causing the issue
objOutput = t1.ConvertCase(obj);
return objOutput;
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
t1.OnProgressUpdate += t1_OnProgressUpdate;
pb.Maximum = 30;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerAsync();
}
private void t1_OnProgressUpdate(int value)
{
Dispatcher.Invoke((Action)delegate
{
pb.Value = value;
});
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
Excel.Range SelectedRange = Globals.ThisAddIn.Application.Selection as Excel.Range;
foreach (Excel.Range rngArea in SelectedRange.Areas)
{
objInput = Common.GetObject(SelectedRange);
object[,] obj = GetOutput(objInput); //-- This throws an error
// rngArea.Value = obj;
backgroundWorker1.CancelAsync();
}
}
catch(Exception ex)
{
throw;
}
}
}
class testClass
{
public delegate void ProgressUpdate(int value);
public event ProgressUpdate OnProgressUpdate;
public object[,] ConvertCase(object[,] objInput)
{
object[,] objOutput = (object[,])objInput.Clone();
for (int row = objInput.GetLowerBound(0); row <= objInput.GetUpperBound(0); row++)
{
for (int col = objInput.GetLowerBound(1); col <= objInput.GetUpperBound(1); col++)
{
objOutput[row, col] = objInput[row, col] is null ? null : objInput[row, col].ToString().ToUpper();
}
Thread.Sleep(200); // To check and see the real time progress update
if (OnProgressUpdate != null)
{
OnProgressUpdate(row);
}
}
return objOutput;
}
}
Upvotes: 0
Views: 87
Reputation: 169160
Since you cannot access properties of controls or other UI elements on a background thread, you should check whether the IsChecked
property is true
before you start the background worker.
Store the value in a bool
variable that you pass to the RunWorkerAsync
method:
private object[,] GetOutput(object[,] obj, bool isChecked)
{
object[,] objOutput = null;
if (isChecked)
objOutput = t1.ConvertCase(obj);
return objOutput;
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
t1.OnProgressUpdate += t1_OnProgressUpdate;
pb.Maximum = 30;
bool isChecked = rdUpper.IsChecked == true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerAsync(isChecked);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Excel.Range SelectedRange = Globals.ThisAddIn.Application.Selection as Excel.Range;
foreach (Excel.Range rngArea in SelectedRange.Areas)
{
objInput = Common.GetObject(SelectedRange);
bool isChecked = (bool)e.Argument;
object[,] obj = GetOutput(objInput, isChecked);
// rngArea.Value = obj;
backgroundWorker1.CancelAsync();
}
}
Upvotes: 0
Reputation: 9384
You are using the BackgroundWorker
false. Your call to ProgressUpdate is called from a background-thread.
You have to subscribe for ProgressChanged
-Event from the BackgroundWorker
. In this methode you can do stuff on the UI-Thread.
Take a look at the following code:
private void Button1_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
backgroundWorker.DoWork += BackgroundWorkerOnDoWork;
backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
backgroundWorker.RunWorkerAsync(t1);
}
private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
{
// This method will be called if in the DoWork-Method a call to ReportProgress is made.
// You can access the provided data with:
object data = e.UserState;
}
private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
{
testClass t1 = (testClass) e.Argument;
// Your async-logic here. Everything here will be executed in a Thread
// If you want to update the UI you have to do the following:
((BackgroundWorker)sender).ReportProgress(0, "OBJECT-TO-PASS-TO-THE-UI-THREAD");
}
Upvotes: 1