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 8925049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:31+00:00 2026-06-15T07:35:31+00:00

I have the following code running in a WPF app: public partial class MainWindow

  • 0

I have the following code running in a WPF app:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        object obj = new object();
        Collection.Add(obj);
        Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(delegate(object sender2, NotifyCollectionChangedEventArgs e2)
        {
            if (Collection.Count == 0)
                App.Current.MainWindow.Close();
        });

        Task.Factory.StartNew(() =>
            {
                //Do long running process
                Collection.Remove(obj); //this errors out
            });
    }

    private ObservableCollection<object> Collection = new ObservableCollection<object>();
}

I get the error System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

I was under the impression that Task.Factory.StartNew queued up an async task, so the thread should be the same, no?

  • 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-06-15T07:35:33+00:00Added an answer on June 15, 2026 at 7:35 am

    Task.Factory.StartNew executes your action in the default TaskScheduler, so it will run in the ThreadPool.

    ObservableCollection is not thread-safe. It means that your CollectionChanged handler, which performs operations on UI controls ( App.Current.MainWindow.Close() ) is not going to be executed in the UI thread because the collection modification is being done in your Task’s action, causing the error you are seeing.

    If you only need to interact with the UI in your handler, you can use the dispatcher:

    Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(delegate(object sender2, NotifyCollectionChangedEventArgs e2)
            {
                if (Collection.Count == 0)
                    this.Dispatcher.BeginInvoke((Action)(()=> App.Current.MainWindow.Close()));
            });
    

    If you need to bind to it, consider using a thread-safe implementation. See this.

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

Sidebar

Related Questions

I have the following code: public void disconnect() { running = false; if(thread !=
I have the following code running in AsyncTask: socket = new Socket(host, Integer.parseInt(port)); In
I have the following code using RapahelJs running in IE. This code cause a
I have following code: Capybara.register_driver :selenium do |app| driver = Capybara::Selenium::Driver.new(app, browser: :firefox, profile:
I have following code. public class SplashScreen extends Activity { private int _splashTime =
I have following code like this in 'artists_helpers' class. def round_to(x) (self * 10**x).round.to_f
I have the following code running in a windows service: WebClient webClient = new
I have following code in an action: render :file => 'public/404.html' and return This
I have the following code running on my Android device. It works great and
I have the following code for running a mongo query, Where the 'mongotester' is

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.