简体   繁体   中英

C# Windows Phone Mango - Invalid cross-thread access? parsing XML

I have the following code which seems to throw a "Invalid cross-thread access." and I can't seem to figure out why. I'm loading a remote xml file from a URL, however, when parsing that XML I always receive this error. Any suggestions?

using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            string xml = streamReader.ReadToEnd();

            using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
            {

                    reader.ReadToFollowing("channel");
                    reader.MoveToFirstAttribute();

                    reader.ReadToFollowing("title");
                    output.AppendLine("Title: " + reader.ReadElementContentAsString());

                    reader.ReadToFollowing("description");
                    output.AppendLine("Desc: " + reader.ReadElementContentAsString());

                    textBox1.Text = output.ToString(); //Invalid cross-thread access.
            }

        }

The XML I'm trying to parse looks like the following, I'm just trying to parse bits and pieces as I continue to learn how to use c# to parse different types of XML:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"xmlns:dc="http://purl.org   /dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0  /modules/slash/">
  <channel>
<title>Server &amp; Site News</title>
<description>A place for the Admin and Moderators to post the latest news on both the server and site.</description>
<pubDate>Fri, 18 May 2012 22:45:08 +0000</pubDate>
<lastBuildDate>Fri, 18 May 2012 22:45:08 +0000</lastBuildDate>
<generator>Forums</generator>
<link>http://removedurl.com/forums/server-site-news.23/</link>
<atom:link rel="self" type="application/rss+xml" href="http://removedurl.com/forums/server-site-news.23/index.rss"/>
<item>
  <title>Example Title</title>
  <pubDate>Mon, 14 May 2012 17:39:45 +0000</pubDate>
  <link>http://removedurl.com/threads/back-fully-working.11013/</link>
  <guid>http://removedurl.com/threadsback-fully-working.11013/</guid>
  <author>Admin</author>
  <dc:creator>Admin</dc:creator>
  <slash:comments>14</slash:comments>
</item>
</channel>

textBox1.Text = output.ToString(); //Invalid cross-thread access.

you get that because you are calling UI thread while you are doing operations on IO thread. Try separating those operations or call invoke on the UI thread.

try changing your code to something like this.

Dispatcher.BeginInvoke( () => { //your ui update code } );

To add a bit more detail from Mayank:

Dispatcher.BeginInvoke( () => {
  textBox1.Text = output.ToString()
} );

You have to marshal the call to UI objects back to the UI thread. Nothing can modify UI elements on any other thread than the main one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM