Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 823151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:54:27+00:00 2026-05-15T02:54:27+00:00

I added an event handler to my code and it broke all access to

  • 0

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
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:54:28+00:00Added an answer on May 15, 2026 at 2:54 am

    Ok, CollectionViewSource derived classes, BindableList, ObservableCollection etc these classes can only be created in main dispatcher thread only.

    However you have to try something of following sort,
    Create your collectionviewsource only in your WPF derived classes, use List<> classes to load your objects in different thread and once done, you can transfer from list to collectionviewsource as follow, I would recommend BindingList because you can add multiple items disabling the refresh to remove flickering.

    Create your collection object implicitly in your WPF classes as follow

    public class MyWindow : UserControl{
    BindingList<MyObject> ObjectList = new BindingList<MyObject>;
        public MyWindow(){
            ObjectList.AllowAdd = true;
            ObjectList.AllowDelete = true;
            ObjectList.AllowEdit = true;
        }
        public void LoadObjects(){
           ThreadPool.QueryUserItem( (s)=>{
               // load your objects in list first in different thread
               List<MyObject> list = MyLongMethodToLoadObjects();
               Dispatcher.BeginInvoke( (Action)delegate(){
                   list.RaiseEvents = false;
                   foreach(MyObject obj in list){
                       ObjectList.Add(obj);
                   }
                   list.RaiseEvents = true;
                   list.ResetBindings();
               });
           });
        }
    }
    

    I dont know this code does not format correctly but you may try seeing it in visual studio to get correct idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I added an event handler to my code and it broke all access to
I have code like this: // Create event handler delegate symbolReader.ReadNotify += new EventHandler(symbolReader_ReadNotify);
I added an event handler to the WebClient's DownloadProgressChanged event, but it never seems
Why does an event handler never get called if it's added within a loop
I've successfully added a handler to the click event of polylines I've added to
In the adapter class of Listview i have added setOnClickListener event for each of
I was using Events in my class and added an event reference to the
Will the events that I added to the global event bus deleted by calling
I have an Event Handler that overrides the ItemAdded method. This works fine on
I have a code like this where I am trying to add an event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.