Thread Locking CollectionViewSource

Posted by Robert on Stack Overflow See other posts from Stack Overflow or by Robert
Published on 2010-05-30T20:26:34Z Indexed on 2010/05/30 20:32 UTC
Read the original article Hit count: 239

Filed under:
|
|

I added an event handler to my code and it broke all access to the CollectionViewSources in the SystemHTA class saying "The calling thread cannot access this object because a different thread owns it". My class was working when "this.systemHTA = new SystemHTA();" was placed outside of the DeviceManager_StateChanged() function.

    public partial class MainWindow : Window
    {
        private DeviceManager DeviceManager = DeviceManager.Instance;
        public SystemHTA systemHTA;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DeviceManager.StateChanged += new EventHandler<DeviceManagerStateChangedEventArgs>(DeviceManager_StateChanged);
            DeviceManager.Initialize();
        }

        void DeviceManager_StateChanged(object sender, DeviceManagerStateChangedEventArgs e)
        {
            if (e.State == DeviceManagerState.Operational)
            {
                this.systemHTA = new SystemHTA();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.systemHTA.GetViewSourceTest();
        }
    }


    public class SystemHTA
    {
        private CollectionViewSource _deviceTestSource;

        public SystemHTA()
        {
            _deviceTestSource = new CollectionViewSource();
            _deviceTestSource.Source = CreateLoadData<HWController>.ControllerCollection;
        }

        public void GetViewSourceTest()
        {
            ListCollectionView view = (ListCollectionView)_deviceTestSource.View; //This creates an error saying a thread already owns _deviceTestSource
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf