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
Your thread does nothing useful - by using _dispatcher.BeginInvoke
to run your search you are effectively executing the search on the UI thread, which blocks your animation. Use dispatcher from your background thread only for operations that manipulate UI controls or that cause PropertyChanged events to be fired.
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();
}