title: "The calling thread cannot access this object because a different thread owns it." c#
body: <p>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.</p>
  
  <p>Here is the AddDirectory method:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Adds all the movies in the specified directory and all its subdirectories to the database.
      /// &lt;/summary&gt;
      /// &lt;param name="path"&gt;A string representing the directory path.&lt;/param&gt;
      /// &lt;returns&gt;True if all the files were added successfully, false otherwise.&lt;/returns&gt;
      /// &lt;exception cref="System.ArgumentException"&gt;Thrown if the path does not lead to a directory.&lt;/exception&gt;
      public static bool AddDirectory(string path)
      {
          if (!FileProcessing.IsDirectory(path))
          {
              return false;
          }
  
          List&lt;string&gt; 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(() =&gt;
          {
              foreach (string filePath in filePaths)
              {
                  AddMovie(filePath);
              }
          });
  
          //make the thread STA and start it
          thread.SetApartmentState(ApartmentState.STA);
          thread.Start();
  
          return true;
      }
  </code></pre>
  
  <p>In the same class, I have the following event and delegate:</p>
  
  <pre><code>    public delegate void MovieAddedHandler(MovieEventArgs e);
  
      /// &lt;summary&gt;
      /// Called on when a movie is inserted into the database.
      /// &lt;/summary&gt;
      public static event MovieAddedHandler MovieAdded;
  </code></pre>
  
  <p>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.</p>
  
  <p>Now the GUI updating is where I'm having difficulty. </p>
  
  <p>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. </p>
  
  <pre><code>MovieInsertion.MovieAdded += (e2) =&gt;
  {
      this.movies = MovieDataRetrieval.GetMovies();
      this.labels.Clear();
      this.InitializeMovieLabels();
  };
  </code></pre>
  
  <p>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.</p>
  
  <p>The error itself is called in the InitializeMovieLabels() method:</p>
  
  <pre><code>foreach (Label labelIterator in labels)
  {
      this.grid.Children.Add(labelIterator);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>The error I get is (as described in the title): "The calling thread cannot access this object because a different thread owns it."</p>
  
  <p>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 :).</p>
  
  <p>Any help would be appreciated.</p>