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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:03:15+00:00 2026-06-05T09:03:15+00:00

I’m designing a media player, and I have a method called AddDirectory, which adds

  • 0

I’m designing a media player, and I have a method called AddDirectory, which adds all the movies in a specified directory to the media player’s database. This method takes a while to process, and so I decided to make it run in the background so the user can keep using the program.

Here is the AddDirectory method:

    /// <summary>
    /// Adds all the movies in the specified directory and all its subdirectories to the database.
    /// </summary>
    /// <param name="path">A string representing the directory path.</param>
    /// <returns>True if all the files were added successfully, false otherwise.</returns>
    /// <exception cref="System.ArgumentException">Thrown if the path does not lead to a directory.</exception>
    public static bool AddDirectory(string path)
    {
        if (!FileProcessing.IsDirectory(path))
        {
            return false;
        }

        List<string> filePaths = FileProcessing.GetDirectoryMovieFiles(path); //a list containing the paths of all the movie files in the directory

        //add the movie in a separate thread so as to not interrupt the flow of the program
        Thread thread = new Thread(() =>
        {
            foreach (string filePath in filePaths)
            {
                AddMovie(filePath);
            }
        });

        //make the thread STA and start it
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();

        return true;
    }

In the same class, I have the following event and delegate:

    public delegate void MovieAddedHandler(MovieEventArgs e);

    /// <summary>
    /// Called on when a movie is inserted into the database.
    /// </summary>
    public static event MovieAddedHandler MovieAdded;

I need this event so that the GUI knows when a new movie has been added to the database, and so it can update the GUI and notify the user accordingly. So when I add a directory with, say, 50 movies, the event is called on 50 times.

Now the GUI updating is where I’m having difficulty.

I have the following code segment, which is part of a method that is called on whenever the user clicks on the “Add Directory” label in the GUI.

MovieInsertion.MovieAdded += (e2) =>
{
    this.movies = MovieDataRetrieval.GetMovies();
    this.labels.Clear();
    this.InitializeMovieLabels();
};

The GetMovies() method returns a List of all the Movies in the database (Represented by a separate Movie class). I then clear all the labels in the GUI grid, then initialize them again, so that each time a movie is added, the user can access the movie immediately in the program, without having to wait for the rest of the movies in the directory to be added.

The error itself is called in the InitializeMovieLabels() method:

foreach (Label labelIterator in labels)
{
    this.grid.Children.Add(labelIterator);
}

The “labels” variable is a List of all the Labels that represent the movies in the database. I want to add each label to the grid.

The error I get is (as described in the title): “The calling thread cannot access this object because a different thread owns it.”

I’m sort of (very) inexperienced with threads, and I tried searching for a solution but was unsuccessful. Sorry if I went a little overboard with the details :).

Any help would be appreciated.

  • 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-05T09:03:19+00:00Added an answer on June 5, 2026 at 9:03 am

    Use MethodInvoker where you updating GUI from different thread.

    Your labels.clear() statement try to update GUI from different thread. So use this type of code.

    if(this.labels.InvokeReqiured)
    {
        this.labels.Invoke( (MethodInvoker) ( ()=> this.labels.Clear() ) );
    }
    else
    this.labels.Clear();
    

    Where you want to update GUI from different thread make sure to use MethodInvoker delegate.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.