0

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 ?

Vynz
  • 63
  • 12
  • You should use [Control.Invoke](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invoke?view=netframework-4.8) or [Control.BeginInvoke](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.begininvoke?view=netframework-4.8). – Alessandro D'Andria Feb 20 '20 at 13:12
  • 1
    Does this answer your question? [How do I update the GUI from another thread?](https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread) – MickyD Feb 20 '20 at 13:17
  • I'll check these possibilities and come back later, thanks – Vynz Feb 20 '20 at 13:27
  • You really shouldn't Invoke from the `DoWork` event (ever). If you need values that belong to UI Controls, pass these values in the [`RunWorkerAsync(object argument)`](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.runworkerasync?view=netframework-4.8#System_ComponentModel_BackgroundWorker_RunWorkerAsync_System_Object_) overload. You can pass whatever you need to use in the `DoWork` event (usually, a class object that stores values coming from the UI Thread in properties). You can then access the `object` reading `e.Argument` (casting as required). – Jimi Feb 20 '20 at 14:26

1 Answers1

1

Use the dispatcher:

void myLongLastingFunction(0)
{
    Dispatcher.BeginInvoke((Action)(() => {
        line_list.SelectedIndex = 0;
    }));
}
paul
  • 20,643
  • 1
  • 48
  • 53