I try to draw something on canvas every 1 second .Then, I create a thread to call a method to draw after sleep 1 second. The problem is calling Canvas object to draw. The program tell me error message
The calling thread cannot access this object because a different thread owns it.
Code
private void drawTimeTick() {
...
Thread iThread = new Thread(new ThreadStart(tickThread));
iThread.Start();
}
private void tickThread(){
try
{
Thread.Sleep(1000);
...
Canvas.SetLeft(tick, 700);
Canvas.SetTop(tick, 30);
}catch(Exception ex){
MessageBox.Show("Exception tickThread : "+ex.Message);
}
}
How to access Canvas in Thread to draw?
Have a look at the DispatcherTimer class. It is designed for exactly this scenario, as can be seen in the MSDN examples.