【问题标题】:Start/Stop DispatcherTimer from a different thread从不同的线程启动/停止 DispatcherTimer
【发布时间】:2011-10-10 07:22:56
【问题描述】:

这是我的代码..

public DispatcherTimer tmr = new DispatcherTimer();

void somefunction (parameters){

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

   }

我的问题是我无法从第二个函数访问 tmr 对象的 Start/Stop 方法,因为它在不同的线程上运行!!!

有人可以帮帮我吗?我被这个问题困扰了将近 3 天! :(

【问题讨论】:

    标签: c# silverlight windows-phone-7 silverlight-4.0 windows-phone-7.1


    【解决方案1】:

    您需要像这样通过 Dispatcher 调用它(用于编组来自另一个线程的调用)

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

    【讨论】:

    • 感谢hasan..但由于“tmr”已经是一个DispatcherTimer 对象,它没有Dispatcher 方法!如果 tmr 只是一个计时器对象,这会起作用,但它不是.. :(
    • 还是不行。我得到的错误是.. System.Windows.Threading.Dispatcher'不包含'Invoke'的定义,并且没有扩展方法'Invoke'接受'System.Windows.Threading.Dispatcher'类型的第一个参数(您是否缺少 using 指令或程序集引用?)
    • @Gowtham 试试 begininvoke
    • 我尝试将 Isenabled 设置为 true 或 false。但这也不起作用。你能告诉我如何使用 BeginInvoke 吗?我非常感谢您对此感兴趣....:)
    • +1 但是实际上不需要将 () 的 lambda 转换为 Action,因为 timer.Start 与 Action 具有相同的签名,因此不需要 lambda一点也不。这将起作用:Deployment.Current.Dispatcher.BeginInvoke(timer.Start);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    相关资源
    最近更新 更多