Dispatcher.RunAsync (The application called an interface that was marshalled for a different thread.)

0

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

enter image description here

In one of my Views I instantiate an options view within it.

Not sure if the options of should be an entirely separate object or not.

When the user modifies the options I want it to update the main view.

First View that is instantiated.

    public VideosFoundView()
    {
        this.InitializeComponent();
        this.initAddVideoFolderGridView();
        this.addVideoFolderScrollViewer.ViewChanged += addVideoFolderScrollViewer_viewChanged;
        options = new vfvOptions();
        options.PropertyChanged += optionsChanged;
        AppShell.Current.SetOptions(options);
    }

As you can tell I instantiate the vfvOptions view within the constructor of the parent view, however the optionsChanged method is having difficulty accessing something I define within the parent.

    private async void optionsChanged(object sender, PropertyChangedEventArgs e)
    {
        await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
        {           
            //parse the options object   
            await Task.Run(() => 
            {
                this._GridViewVideoModels.Clear();
                for (int i = 0; i < VideosFoundView.MyVideoModels.Count - 1; i++)
                {
                    bool displayTile = true;

                    if (options.IgnoreImagesNotFound)
                        if (VideosFoundView.MyVideoModels[i].FullImageLocationOnDisk == "ms-appx:///Assets/image-not-found.gif")
                            displayTile = false;

                    if (displayTile)
                    {
                        this._GridViewVideoModels.Add(VideosFoundView.MyVideoModels[i]);                        
                    }
                }                   
                this.AddVideoFolderGridView.ItemsSource = _GridViewVideoModels;                                
            });
        });
    }

The above code hits the exception at:

   this._GridViewVideoModels.Clear();

Which is defined in the ViewFoundView class like so:

    public ObservableCollection<MyVideo> _GridViewVideoModels = new ObservableCollection<MyVideo>();

I may not have a thorough understanding of events, when the event is triggered is the method optionsChanged triggered from within the vfvOptions class/object?

c#
multithreading
winforms
asked on Stack Overflow Mar 24, 2016 by Illuminati

1 Answer

0

The problem was because I called the method from within the Task.Run()

answered on Stack Overflow Mar 24, 2016 by Illuminati

User contributions licensed under CC BY-SA 3.0