Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The application called an interface that was marshalled for a different thread in window 8

In project i use HttpWebRequest to to download Feeds :

HttpWebRequest request;
        public void getFeeds()
        {
            request = (HttpWebRequest)WebRequest.Create("http://vnexpress.net/rss/gl/trang-chu.rss");
            request.BeginGetResponse(ResponseCallBack, request);
        }
        SampleDataGroup group1;
        private void ResponseCallBack(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState;
            var response = request.EndGetResponse(result) as HttpWebResponse;


            if (response != null && response.StatusCode == HttpStatusCode.OK)
            {
                var stream = response.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    string strReaderReader = reader.ReadToEnd();

                    XDocument doc = XDocument.Parse(strReaderReader);
                    var element = doc.Root.Element("channel").Elements("item");
                    group1 = new SampleDataGroup("Group-1",
                    "Hot News",
                    "Group Subtitle: 1",
                    "Assets/DarkGray.png",
                    "");
                    foreach (XElement x in element)
                    {

                        group1.Items.Add(new SampleDataItem("", x.Element("title").Value, "", "", "", "", group1));

                    }
                    request.Abort();

                    request = null;
                    _allGroups.Add(group1);

                    onComplete1();
                }
            }
        }

onComplete1() is delegate , i use to update layout when pares XMl complete. But it erros :

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

Please help me !

like image 691
thongaduka Avatar asked Dec 07 '22 13:12

thongaduka


1 Answers

Most likely, your onComplete1() method does some UI access.

Try to enclose it in a Dispatcher.RunAsync method:

this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
      onComplete1();
    });

or await it as a Task, depending on your needs:

this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
      onComplete1();
    }).AsTask().Wait();
like image 163
Magnus Johansson Avatar answered Jan 04 '23 23:01

Magnus Johansson