title: WPF/c#: How to solve thread problem while using SplashScreen / Loading Screen
body: <p>Due to some of my page windows will loading big amount of calculation before it shown, hence I created a WPF Window and make it to be a Loading Screen with GIF to show before the page rendered. </p>
  
  <p>When I want the loading screen come out , I use this code :</p>
  
  <blockquote>
    <p>Loading.ShowSplashScreen();</p>
  </blockquote>
  
  <p>After the page loaded, I will use the code below to close the loading screen</p>
  
  <blockquote>
    <p>Loading.CloseForm();</p>
  </blockquote>
  
  <p>And the code behind of loading page are in the following:</p>
  
  <pre><code>public partial class Loading : Window
  {
      private static Loading LD;
      public static Dictionary&lt;string, object&gt; Dic = new Dictionary&lt;string, object&gt;();
  
      public Loading()
      {
          InitializeComponent();
      }
  
      static public void ShowSplashScreen()
      {
          Thread t = new Thread(() =&gt;
          {
              Loading sw = new Loading();
              Dic["Loading"] = sw;
                  sw.ShowDialog();
              });
          t.SetApartmentState(ApartmentState.STA);
          t.Start();
      }
  
      static public void CloseForm()
      {
          if (Loading.Dic.ContainsKey("Loading"))
          {
              Loading sw = Loading.Dic["Loading"] as Loading;
              sw.Dispatcher.Invoke((Action)(() =&gt; sw.Close()));
          }
      }
  }
  </code></pre>
  
  <p>It's no problem when I "First Times" call out the loading screen, but I will having an error once I "Second Times" call out the loading screen and the error in the following :</p>
  
  <blockquote>
    <p>The calling thread cannot access this object because a different thread owns it.</p>
  </blockquote>
  
  <p>May anyone can help me to see how to solve this problem ?
  Many Thanks !</p>