1

getting right to it here is a few lines from the code I have in my code behind:

private static Factual factual = new Factual(FACTUAL_KEY, FACTUAL_SECRET);

private void OnStartQueries_Click(object sender, RoutedEventArgs e)
{
    RunAsyncQuery(43.0120, -81.2003, 3000, 25).ContinueWith(task => rtbJsonData.AppendText(task.Result));
}

private static async Task<string> RunAsyncQuery(double lat, double lng, int radius, int limit)
{
    return await Task.Run(() => factual.Fetch("places", new Query().WithIn(new Circle(lat, lng, radius)).Limit(limit)));
}

So as you can see when I click the Start Queries button I want to run a method asynchronously. When it returns with the result from that query, I want to set the text of the RichTextBox "rtbJsonData" to the result. However, currently when I run it I get the exception:

"The calling thread cannot access this object because a different thread owns it."

How can I do this?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Booster
  • 140
  • 9

1 Answers1

0

The RichTextBox "rtbJsonData" is being owned by the main thread and cannot be accessed from another thread directly.

In WPF, your model can you can derive from the INotifyPropertyChanged interface. Any changes on the model will be updated on the view.

See MSDN for a full example: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

This is based on the MVVM (Model View View-Model) pattern, https://msdn.microsoft.com/en-us/library/hh848246.aspx

Kevin
  • 506
  • 6
  • 13