WPF如何从后台工作程序中的线程停止动画

时间:2010-03-22 16:39:49

标签: wpf animation backgroundworker

我的任务需要很长时间。我使用后台工作线程并在启动它之前,因为Do_Work我在标签上开始动画,当任务完成时,我在RunWorkerCompleted中停止它但我收到错误因为我尝试在后台线程中开始/停止动画不是所有者。我怎么能这样做,我的意思是在后台工作者中开始/停止动画?

谢谢!

3 个答案:

答案 0 :(得分:1)

您应该在开始BackgroundWorker之前启动动画,而不是在DoWork事件中。这样,您就可以从RunWorkerCompleted事件中停止它。

答案 1 :(得分:0)

您需要在执行动画的控件上使用Dispatcher.BeginInvoke方法。

答案 2 :(得分:0)

您还可以使用以下内容在UI线程上调用停止动画:

private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    /* If not operating on the main UI thread, call this method again on the App dispatcher's thread */
    if (App.Current != null && App.Current.Dispatcher.Thread != Thread.CurrentThread)
    {
        App.Current.Dispatcher.Invoke(new RunWorkerCompletedEventHandler(OnRunWorkerCompleted), new object[] { sender, e});
        return;
    }

    // Do stuff to the UI here
}
相关问题