简体   繁体   中英

(Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

I try to associate a ObservableCollection to a ListBox, but I get the following error: The application called an interface That was marshalled for a different thread. ( Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD) )

Prueba.cs In class, I have:

  public ObservableCollection<WidgetCollectionItem> WidgetsDownloaded(string e)
    {
        int alertas = 0;

        XDocument document = XDocument.Parse(e);

        // Obtener el id
        //id = document.Root.Element("id").Value;

        ObservableCollection<WidgetCollectionItem> Items = new ObservableCollection<WidgetCollectionItem>();
        foreach (XElement wid in document.Root.Elements("widget"))
        {
            WidgetCollectionItem widget = new WidgetCollectionItem();
            widget.captionWid = wid.Attribute("caption") != null ? wid.Attribute("caption").Value : null;

            ObservableCollection<ServiciosWidgetCollectionItem> ItemsSer = new ObservableCollection<ServiciosWidgetCollectionItem>();
            widget.ItemsSer = ItemsSer;

            foreach (XElement service in wid.Elements("service"))
            {
                ServiciosWidgetCollectionItem ser = new ServiciosWidgetCollectionItem();

                ser.captionWid = wid.Attribute("caption") != null ? wid.Attribute("caption").Value : null;
                ser.nameWid = wid.Attribute("name") != null ? wid.Attribute("name").Value : null;
                ser.nameSer = service.Attribute("name") != null ? service.Attribute("name").Value : null;
                ser.captionSer = service.Attribute("caption") != null ? service.Attribute("caption").Value : null;
                ser.descripcion = service.Attribute("desc") != null ? service.Attribute("desc").Value : null;
                ser.valor = service.Element("xvalue") != null ? service.Element("xvalue").Value : null;
                ser.color = service.Element("xcolor") != null ? service.Element("xcolor").Value : null;
                ser.alerta = service.Element("xalert") != null ? service.Element("xalert").Value : null;
                if (ser.alerta != null) { alertas++; }

                ItemsSer.Add(ser);
            }
            Items.Add(widget);
        }
        return Items;
    }




 public class ServiciosWidgetCollectionItem
    {
        public string captionWid { get; set; }
        public string captionSer { get; set; }
        public string nameWid { get; set; }
        public string nameSer { get; set; }
        public string descripcion { get; set; }
        public string valor { get; set; }
        public string color { get; set; }
        public string alerta { get; set; }
    }

    public class WidgetCollectionItem
    {
        public string captionWid { get; set; }
        public ObservableCollection<ServiciosWidgetCollectionItem> ItemsSer { get; set; }
    }

And from the MainPage make the connection to the page, the whole process works fine, but when I insert a ListBox ItemsSouces gives me the error:

public MainPage()
    {
        this.InitializeComponent();

        Prueba prueba = new Prueba();

        var request = (HttpWebRequest)WebRequest.Create(new Uri(ruta));
        request.BeginGetResponse(r =>
        {
            var httpRequest = (HttpWebRequest)r.AsyncState;
            var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);

            using (var reader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var response = reader.ReadToEnd();

                widgetsList.ItemsSource = prueba.WidgetsDownloaded(response);
            }
        }, request);
    }

widgetsList.ItemsSource is only allowed to be accessed from the thread used to create the control. This is normally always the UI thread. On MSDN you can read about BeginGetResponse that it starts an asynchronous request, so your delegate is called on a different thread. Assign the response in the original thread (UI thread) and you're fine.

Edit: See Run code on UI thread in WinRT for an example how to achieve this. Note: save the dispatcher on startup in a static variable to make your life easier.

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