title: Initialization of 'System.Windows.Controls.Button' threw an exception
body: <p>I am creating a WPF on which, i have 2 threads. One is the main, and the other one(called PillTimeOutChecker) check some requirements in the main form. If the requirements meet, a new form through the PilleCheckerThread show up in a new thread. The problem is that i am getting this error: Initialization of 'System.Windows.Controls.Button' threw an exception in the initialization of the new form.</p>
  
  <p>This is the method in the main thread, that call the PillCheckerThread:</p>
  
  <pre><code>private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          Thread PillChecker = new Thread(new ThreadStart(PillCheckerThread));
          PillChecker.SetApartmentState(ApartmentState.STA);
          PillChecker.IsBackground = true;
          PillChecker.Name = "PillTimeOutChecker";
          PillChecker.Start();
      }
  </code></pre>
  
  <p>This is the content of the PillCheckerThread method: </p>
  
  <pre><code> private void PillCheckerThread()
      {
              foreach (DataGridObject item in PillList.Items)
              {
                 if(item.DoIt)
                 {
                          //Show PillWarning window
                          Thread PillWarningWindow = new Thread(new ThreadStart(() =&gt;
                          {
                               PillWarningWindow pl = new PillWarningWindow(item.PillName, item.AlertTime);
                               pl.Show();
                                   System.Windows.Threading.Dispatcher.Run();
                              }));
                          PillWarningWindow.SetApartmentState(ApartmentState.STA);
                          PillWarningWindow.IsBackground = true;
                          PillWarningWindow.Start();
                }
             }
      }
  </code></pre>
  
  <p>This is the content of the PillWarningWindow:</p>
  
  <pre><code>public partial class PillWarningWindow : Window
  {
      public PillWarningWindow(string PillName, string CurrentTime)
      {
          InitializeComponent();
          PillNameLbl.Content = PillName;
          TimeLbl.Content = CurrentTime;
      }
  
      private void CloseBtn_Click(object sender, RoutedEventArgs e)
      {
          this.Close();
      }
  }
  </code></pre>
  
  <p>This is the xaml of the PillWarningWindow:</p>
  
  <pre><code>&lt;Window x:Class="MedicalReminder.PillWarningWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:MedicalReminder"
      mc:Ignorable="d" Height="300" Width="713.414" ShowInTaskbar="False" Topmost="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="Transparent" AllowsTransparency="True"&gt;
  &lt;Border BorderBrush="Black" CornerRadius="20" Background="DarkCyan"&gt;
      &lt;Grid&gt;
          &lt;Button x:Name="CloseBtn" Content="OK" HorizontalAlignment="Left" Margin="262,239,0,0" VerticalAlignment="Top" Width="179" Click="CloseBtn_Click"/&gt;
          &lt;Label x:Name="label" Content="Psssttt !! It's time to take your pill: " HorizontalAlignment="Left" Margin="89,93,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/&gt;
          &lt;Label x:Name="PillNameLbl" Content="PillName" HorizontalAlignment="Left" Margin="395,93,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold"/&gt;
          &lt;Label x:Name="label2" Content="It's exactly " HorizontalAlignment="Left" Margin="89,132,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/&gt;
          &lt;Label x:Name="TimeLbl" Content="12:00" HorizontalAlignment="Left" Margin="195,133,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="17" Width="56"/&gt;
          &lt;Label x:Name="label3" Content="you forgot it ??" HorizontalAlignment="Left" Margin="256,132,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/&gt;
  
      &lt;/Grid&gt;
  &lt;/Border&gt;
  </code></pre>
  
  <p></p>
  
  <p>With a breakpoint at the constructor of the PillWarningWindow i found out that the error start at the InitializeComponent method. Any help is appreciated. Thanks.</p>