title: The calling thread must be STA, because many UI components require this
body: <p>I found on website all about the answer to that question, the code changes into the following format is no longer an error, but the delegate does not execute the statement inside a, What's wrong? Can anybody help me?My program use multimedia timer every 2 seconds to draw a curve point, draw the curve with Visifire</p>
  
  <pre><code>Thread Messagethread = new Thread(new ThreadStart(delegate() {
                  DispatcherOperation DispacherOP = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, 
                      new Action(delegate() {
                      ChartData.Add(new Tuple&lt;string, double&gt;("A" + seed.NextDouble(), 1.5 + seed.Next(10)));
                  })); 
              }));
              Messagethread.SetApartmentState(ApartmentState.STA);
              Messagethread.Start();
  </code></pre>
  
  <p>My ViewModel page:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows.Input;
  using System.Windows;
  using System.Collections.ObjectModel;
  using Dongzr.MidiLite;
  using System.Windows.Threading;
  using System.Threading;
  
  namespace WpfVisifire
  {
      public class ChartViewModel
      {
          static MmTimer timer1;
          static DispatcherTimer timer2;
  
          private static readonly Random seed = new Random();
          public ObservableCollection&lt;Tuple&lt;string, double&gt;&gt; ChartData
          {
              get;
              private set;
          }
  
          public ChartViewModel()
          {
              StopDataCommand = new RelayCommand((p) =&gt; stop());
              ChangeVisiChartDataCommand = new RelayCommand((p) =&gt; changeData());
              ChartData = new ObservableCollection&lt;Tuple&lt;string, double&gt;&gt;();
              timer1 = new MmTimer();
          }
          public ICommand StopDataCommand
          {
              get;
              private set;
          }
          public ICommand ChangeVisiChartDataCommand
          {
              get;
              private set;
          }
          private void changeData()
          {
              timer1.Mode = MmTimerMode.Periodic;
              timer1.Interval = 2000;
              timer1.Tick += new EventHandler(timer1_Tick);
              timer1.Start();           
          }
  
          void timer1_Tick(object sender, EventArgs e)
          {
             /*Dispatcher.CurrentDispatcher.BeginInvoke(
                   DispatcherPriority.Normal,
                   new Action(
                       delegate()
                       {
                           ChartData.Add(new Tuple&lt;string, double&gt;("A" + seed.NextDouble(), 1.5 + seed.Next(10)));
                       }));*/
              Thread Messagethread = new Thread(new ThreadStart(delegate() {
                  DispatcherOperation DispacherOP = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, 
                      new Action(delegate() {
                      ChartData.Add(new Tuple&lt;string, double&gt;("A" + seed.NextDouble(), 1.5 + seed.Next(10)));
                  })); 
              }));
              Messagethread.SetApartmentState(ApartmentState.STA);
              Messagethread.Start(); 
          }
  
          private void stop()
          {
              timer1.Stop();
              timer1.Dispose();
              //.Show("jeighier");
          }
  
      }
  }`
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="WpfVisifire.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vCharts="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"
          xmlns:vm="clr-namespace:WpfVisifire"
          Title="MainWindow" Height="600" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;vm:ChartViewModel x:Key="chartViewModel" /&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;StackPanel DataContext="{Binding Source={StaticResource chartViewModel}}"&gt;
              &lt;WrapPanel Orientation="Horizontal"&gt;
                  &lt;Button Content="Start" Height="28" Name="Add" Margin="5" Width="125" Command="{Binding Path=ChangeVisiChartDataCommand}"/&gt;
                  &lt;Button Margin="5" Height="28" Width="125" Content="Stop" Command="{Binding Path=StopDataCommand}" /&gt;
              &lt;/WrapPanel&gt;
  
              &lt;vCharts:Chart Watermark="False" Theme="Theme1" Width="480" Height="479" x:Name="MyChart"
                              AnimationEnabled="True" AnimatedUpdate="True"&gt;
                  &lt;vCharts:Chart.Titles&gt;
                      &lt;vCharts:Title Text="This is a chart" FontSize="12" /&gt;
                      &lt;vCharts:Title Text="This is another chart" FontSize="10" HorizontalAlignment="Right" /&gt;
                  &lt;/vCharts:Chart.Titles&gt;
                  &lt;vCharts:Chart.AxesX&gt;
                      &lt;vCharts:Axis Title="horizontal title" /&gt;
                  &lt;/vCharts:Chart.AxesX&gt;
                  &lt;vCharts:Chart.AxesY&gt;
                      &lt;vCharts:Axis Title="vertical title" /&gt;
                  &lt;/vCharts:Chart.AxesY&gt;
                  &lt;vCharts:Chart.Series&gt;
                      &lt;vCharts:DataSeries x:Name="dataSeries" RenderAs="Line"  DataSource="{Binding Path=ChartData}"&gt;
                          &lt;vCharts:DataSeries.DataMappings&gt;
                              &lt;vCharts:DataMapping MemberName="AxisXLabel" Path="Left" /&gt;
                              &lt;vCharts:DataMapping MemberName="YValue" Path="Right" /&gt;
                          &lt;/vCharts:DataSeries.DataMappings&gt;
                      &lt;/vCharts:DataSeries&gt;
                  &lt;/vCharts:Chart.Series&gt;
              &lt;/vCharts:Chart&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>