Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start/Stop DispatcherTimer from a different thread

Here is my code..

public DispatcherTimer tmr = new DispatcherTimer();

void somefunction (parameters){

if (something)
  tmr.Start();
if (something else)
  tmr.Stop();

   }

My problem is that I can't access the Start/Stop methods of the tmr object from the second function since it runs on a different thread!!!

Can somebody please help me?? I am struck wih this problem for almost 3 days! :(

like image 468
Gowtham Avatar asked Oct 10 '11 07:10

Gowtham


1 Answers

You need to Invoke it via Dispatcher (for marshaling the call from another thread) like so

Deployment.Current.Dispatcher.BeginInvoke((Action)(()=>timer.Start())
like image 120
Muhammad Hasan Khan Avatar answered Sep 28 '22 21:09

Muhammad Hasan Khan