另一个线程运行时,WPF动画停止 [英] WPF animation stops when another thread running

查看:339
本文介绍了另一个线程运行时,WPF动画停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口,显示在另一个线程运行的工作的动画。该窗口显示,我可以看到进度条,但动画被冻结。在code在视图模型上运行,并在构造函数中创建调度:

I've a window that shows a 'working' animation when another thread is running. The window shows and I can see the progress bar but the animation is frozen. The code runs on a ViewModel, and the dispatcher is created in the constructor:

_dispatcher = Dispatcher.CurrentDispatcher;

在code创建动画和运行过程如下:

The code to create the animation and run the process is as follows:

Working wrk;    
protected void Search()
{
  ImplementSearch();

  wrk = new Working();
  wrk.Owner = (MainWindow)App.Current.MainWindow;
  wrk.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  wrk.HeadingMessage = "Searching...";
  wrk.UpdateMessage = "Running your search";
  wrk.ShowDialog();      
}

void ImplementSearch()
{
  System.Threading.Thread thread = new System.Threading.Thread(
    new System.Threading.ThreadStart(
      delegate()
      {
        System.Windows.Threading.DispatcherOperation
          dispatcherOp = _dispatcher.BeginInvoke(
          System.Windows.Threading.DispatcherPriority.Normal,
          new Action(
            delegate()
            {                  
              ResetSearch();

              string ret = _searchlogic.PerformSearch(SearchTerm, ref _matchingobjects, TypeOfFilter());
              if (ret != null)
                SearchMessage = ret;

              if (_matchingobjects.Count > 0)
              {
                DataRow row;
                foreach (SearchLogicMatchingObjects item in _matchingobjects)
                {
                  row = _dt.NewRow();
                  row["table"] = item.Table;
                  row["pk"] = item.PK;
                  _dt.Rows.Add(row);
                }

                SelectCurrent();
              }          
            }
        ));

        dispatcherOp.Completed += new EventHandler(dispatcherOp_Completed);
      }
  ));

  thread.Start();
}

void dispatcherOp_Completed(object sender, EventArgs e)
{
  wrk.Close();
}

我不明白,为什么动画停止?任何人都可以帮忙吗?
谢谢

I can't figure out why the animation stops? Can anyone help? Thanks

推荐答案

我觉得你想要做的实际工作在后台线程上,而不是一切都元帅到UI线程,这是BeginInvoke的做什么!通过与的BeginInvoke在UI线程上做的一切,你的动画将无法运行。

I think you want to do the actual work on the background thread, not marshal everything to the UI thread, which is what BeginInvoke does! By doing everything on the UI thread with BeginInvoke, your animation won't run.

Working wrk;     
protected void Search() 
{ 
  ImplementSearch(); 

  wrk = new Working(); 
  wrk.Owner = (MainWindow)App.Current.MainWindow; 
  wrk.WindowStartupLocation = WindowStartupLocation.CenterOwner; 
  wrk.HeadingMessage = "Searching..."; 
  wrk.UpdateMessage = "Running your search"; 
  wrk.ShowDialog();       
} 

void ImplementSearch() 
{ 
    Thread thread = new Thread(new ThreadStart( 
      delegate() 
      { 
          // Call to function which changes UI - marshal to UI thread.
          _dispatcher.BeginInvoke((Action)(() => ResetSearch()));

          string ret = _searchlogic.PerformSearch(SearchTerm, ref _matchingobjects, TypeOfFilter()); 

          if (ret != null) 
          {
              // Call to function which changes UI - marshal to UI thread.
              _dispatcher.BeginInvoke((Action<string>)((r) => SearchMessage = r), ret);
          }

          if (_matchingobjects.Count > 0) 
          { 
            DataRow row; 
            foreach (SearchLogicMatchingObjects item in _matchingobjects) 
            { 
              row = _dt.NewRow(); 
              row["table"] = item.Table; 
              row["pk"] = item.PK; 
              _dt.Rows.Add(row); 
            }  

            // Call to function which changes UI - marshal to UI thread.
            _dispatcher.BeginInvoke((Action)(() => SelectCurrent()));
          }           
        } 

        wrk.Close();
  })); 
  thread.Start();
} 

这篇关于另一个线程运行时,WPF动画停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆