mohsen hadaeghi

mohsen hadaeghi

  • NA
  • 3
  • 2.1k

The calling thread cannot access this object because a diffe

May 10 2013 2:43 AM
Hi, I'm Mohsen. I have a bad problem in my code.Please help me for solve my problem.


Button button;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
  var threadstart = new Thread(new ThreadStart(Create));
  threadstart.SetApartmentState(ApartmentState.STA);
  threadstart.Start(); 
  threadstart.Join();

  threadstart = new Thread(new ThreadStart(Attach));
  threadstart.Start();
  threadstart.Join();
}
private void Create()
{
  button = new Button();
  button.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
  button.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
}
private void Attach()

  Dispatcher.BeginInvoke(new Action(() =>
{
  Canvas.SetTop(button, 0); <===== The calling thread cannot access this object because a different thread owns it.
  Canvas.SetLeft(button, 0);
  MyCanvas.Children.Add(button);
}));


Answers (2)