2012-06-27 40 views
4

編組爲一個不同的線程在項目中,我使用的HttpWebRequest來下載的feeds接口:應用程序調用的是在窗口8

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()爲代表,我用它來更新佈局時剝的XMl完整。 但它錯了:

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

請幫幫我!

+0

你有沒有試圖在這裏更新UI的任何代碼? –

回答

15

很可能,您的onComplete1()方法會執行一些UI訪問。

嘗試把它們放在一個Dispatcher.RunAsync方法:

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

或等待它作爲一個任務,根據您的需要:

this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
    { 
     onComplete1(); 
    }).AsTask().Wait(); 
相關問題