c# - Updating Listbox results in " Invalid cross-thread access." -


i building rss feed reader.i created starter app using visual studio.on it's main page added link new pivot page.all rss thing happens in pivot page.now in rss feed listbox,i set list items using following code:

public pivotpage1() {     initializecomponent();     getmethenews();     addtocollection("android going up","techcrunch");     lstbox.datacontext = thecollection; }  private void addtocollection(string p1, string p2) {     thecollection.add(new newsarticle(p1,p2)); } 

here other 2 functions rss fetched server , parsed,but when want add processed entries observablecollection in gettheresponse() method,it results in invalid cross thread access error.any ideas?

code:

private void getmethenews() {     string url = "http://rss.cnn.com/rss/edition.rss";     httpwebrequest webrequest = (httpwebrequest)httpwebrequest.create(url);     webrequest.begingetresponse(gettheresponse, webrequest); }  private void gettheresponse(iasyncresult result) {     httpwebrequest request = result.asyncstate httpwebrequest;     if (request != null)     {         try         {             webresponse response = request.endgetresponse(result);             xdocument doc = xdocument.load(response.getresponsestream());             ienumerable<xelement> articles = doc.descendants("item");             foreach (var article in articles) {                 system.diagnostics.debug.writeline(article);                 try                 {                     system.diagnostics.debug.writeline(article.element("title").value);                     addtocollection(article.element("title").value,"cnn");                 }                 catch (nullreferenceexception e) {                     system.diagnostics.debug.writeline(e.stacktrace);                 }             }          }         catch(webexception e) {             system.diagnostics.debug.writeline(e.stacktrace.tostring());         }     }     else      {      } } 

you need use dispatcher access control non-ui thread:

deployment.current.dispatcher.begininvoke(()=> {       addtocollection(article.element("title").value,"cnn");    }); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -