Start/Stop DispatcherTimer from a different thread

笑着哭i 提交于 2019-12-08 19:16:38

问题


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! :(


回答1:


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

Deployment.Current.Dispatcher.BeginInvoke((Action)(()=>timer.Start())


来源:https://stackoverflow.com/questions/7709638/start-stop-dispatchertimer-from-a-different-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!