【问题标题】:WPF How to stop animation from a thread in background workerWPF如何从后台工作者的线程中停止动画
【发布时间】:2010-03-22 16:39:48
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: wpf animation backgroundworker


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您还可以使用以下方式在 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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-23
          • 1970-01-01
          • 2021-01-14
          • 1970-01-01
          • 2011-07-25
          相关资源
          最近更新 更多