title: C# wpf dispatcher thread
body: <p>I am trying to create a new <code>Thread</code> and put it to sleep in some occasions, but when I do it the main thread sleep, not only the one that I had created. I am using a <code>Dispatcher.BeginInvoke</code> but this is only to "give a permission" from the main thread to access to the method. </p>
  
  <p>It works because it does not give me an <code>InvalidOperationException</code>, but the "focus" of the created thread losses when the linked method start.</p>
  
  <p>I think I should use a <code>ManualResetEvent</code> to wait for the created <code>Thread</code>, but I do not know how doing it. I have been looking for possible solutions but no one works.</p>
  
  <p>I think this should be easy but i cannot do it. The following code is Below:</p>
  
  <pre><code>void EmpujeDispatcher(object objeto)
  {
      this.Dispatcher.BeginInvoke(new Action&lt;object&gt;(Empuje), objeto);
  }
  private void Empuje(object objeto)
  {
      Thread.Sleep(2000); MessageBox.Show("This should not freeze the window");
  
                  Canvas Bacteria = objeto;
                  double PosX = Canvas.GetLeft(Bacteria);//Posición del sender
                  double PosY = Canvas.GetTop(Bacteria);//Lo mismo
                  Bacterias BacteriaInstancia = InstanciaBacterias[Bacteria.Uid];//Se busca la bacteria para relacionarla con al instancia
                  BacteriaInstancia.posX = PosX;
                  BacteriaInstancia.posY = PosY;
                  // BacteriaInstancia.Moverse();
  
                  if (BacteriaInstancia.momemtum &lt;= 0)
                  {
                      Canvas.SetTop(Bacteria, PosY); Canvas.SetLeft(Bacteria, PosX);//Para el empuje
                      dispatcherTimer.Stop();
                  }
  
                  else
                  {   //Rebote:
                      BacteriaInstancia.Posicion();
                      PosX = BacteriaInstancia.posX;
                      PosY = BacteriaInstancia.posY;
                      if (PosX + Bacteria.Width &gt;= CanvasSimulador.Width) { BacteriaInstancia.direccionAnterior = BacteriaInstancia.direccion; BacteriaInstancia.direccion = 1; }
                      if (PosX &lt;= 0) { BacteriaInstancia.direccionAnterior = BacteriaInstancia.direccion; BacteriaInstancia.direccion = 3; }
                      if (PosY + Bacteria.Height &gt;= CanvasSimulador.Height) { PosY = CanvasSimulador.Height - Bacteria.Height; BacteriaInstancia.direccionAnterior = BacteriaInstancia.direccion; BacteriaInstancia.direccion = 2; }
                      if (PosY &lt;= 0) { PosY = 1; BacteriaInstancia.direccionAnterior = BacteriaInstancia.direccion; BacteriaInstancia.direccion = 4; }
                      Canvas.SetTop(Bacteria, PosY); Canvas.SetLeft(Bacteria, PosX);
  
                      BacteriaInstancia.momemtum = Math.Sqrt(Math.Pow(BacteriaInstancia.Vfx, 2) + Math.Pow(BacteriaInstancia.Vfy, 2));
                      ControlFlujo = BacteriaInstancia.momemtum;
  
  
  
  
  }
  private void EmpujeEvent(object sender, MouseButtonEventArgs e)
  {
      Thread TimerClockThread = new Thread(new ParameterizedThreadStart(EmpujeDispatcher));
      TimerClockThread.IsBackground = true;
      TimerClockThread.Start(sender);
  }
  </code></pre>
  
  <p>This is not exacly the code because in this one <code>Dispatcher</code> does not have any sense, if I create the Thread without dispatcher</p>
  
  <pre><code>TimerClockThread = new Thread( new ParameterizedThreadStart(Empuje));
  </code></pre>
  
  <p>It works well... because it's a <code>MessageBox</code>, but in the original I have a lot of code inside of the "Empuje".</p>
  
  <p>Thanks for your attention and hopefully you can help me :)</p>