Basically, I'm getting some data from a WebService, and in the ResponseCallback I'm trying to fill an ObservableCollection with the results I got from the response, but I get an UnauthorizedAccessException
"Invalid cross-thread access" when I try to do so.
What would be the best way to fill said observable collection when I get the result?
Thanks!
This is the code:
public ObservableCollection<Person> People { get; set; }
private void ResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
string responseString = string.Empty;
using (Stream content = response.GetResponseStream())
{
if (request != null && response != null)
{
if (response.StatusCode == HttpStatusCode.OK)
{
XDocument document = XDocument.Load(content);
var people = from p in document.Descendants()
where p.Name.LocalName == "PersonInfo"
select Person.GetPersonFromXElement(p);
foreach (Person person in people)
{
this.People.Add(person); // this line throws the exception
}
}
}
content.Close();
}
}