title: WPF, How can I only one tick using a DispatcherTimer
body: <p>I'd like to have a function that runs once after a few seconds. (The program is not stopped)</p>
  
  <p>So I'm using a <code>DispatcherTimer</code> like this</p>
  
  <pre><code>private void Button_Click(object sender, RoutedEventArgs e)
  {
      TimerStart();
  }
  
  DispatcherTimer Timer1 = new DispatcherTimer();
  int count = 3;
  private void TimerStart()
  {
      Timer1.Interval = new TimeSpan(0, 0, 3);
      Timer1.Tick += MyEvent;
      Timer1.Start();
  }
  
  void MyEvent(object sender, EventArgs e)
  {
      MessageBox.Show(count.ToString());
      // My code
      (sender as DispatcherTimer).Stop();
      count++;
  }
  </code></pre>
  
  <p>In the first <code>ButtonClick</code> it runs <code>MyEvent()</code> in 3 seconds</p>
  
  <p>The second <code>ButtonClick</code> is performed twice and after 3 seconds the <code>MyEvent()</code> is called</p>
  
  <p>And on the third time, <code>MyEvent()</code> the three runs <code>MyEvent()</code></p>
  
  <p>Why does this happen?</p>
  
  <p>So I tried a different approach, using a Thread:</p>
  
  <pre><code>private void Button_Click(object sender, RoutedEventArgs e)
      {
          System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(
              delegate()
              {
                  System.Threading.Thread.Sleep(2000);
                  // my code
                  count++;
              }));
  
          thread.Start();
      }
  </code></pre>
  
  <p>But this way, the error occurs in a portion of the MyCode <code>(this.Usercontrol.Children.RemoveAt(0);)</code></p>
  
  <blockquote>
    <p>System.InvalidOperationException :
    Other threads can not be the thread that owns the object, call the object >access</p>
  </blockquote>
  
  <p>What should I do?</p>
  
  <p>Thank you, regards.</p>