I'm dealing with a thread error using a worker in my function. I already checked about bindings and 'invoke'(?) but I'm quite new to C# and WPF and I don't really understand the way I should solve this. It seems that I'm calling of function from another which is not the "owner".
void myLongLastingFunction(0)
{
line_list.SelectedIndex = 0; //ERROR CrossThread
blablabla..
}
private void btnClick(object sender, RoutedEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoConvertOne;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
private void worker_DoConvertOne(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
//Processing
myLongLastingFunction(0);
//The progress bas is full...
worker.ReportProgress(100, "Done Processing.");
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Converting finished!");
TestProgressBar.Value = 0;
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
TestProgressBar.Value = e.ProgressPercentage;
}
I was inspired by this post.
Error appears when I'm calling myLongLastingFunction() which is crashing my app :
System.InvalidOperationException: 'Calling thread can't reach this object because a other thread is the owner'
Sorry for my translation which is probably faulse but as close to the original message as possible. Have you got any idea ?