WPF animation stops when another thread running

前端 未结 2 720
栀梦
栀梦 2021-01-16 04:48

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

2条回答
  •  旧时难觅i
    2021-01-16 05:32

    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)((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();
    } 
    

提交回复
热议问题