-1

I am using wpf busy indicator and setting its Isbusy property from viewModel. Aftersetting Isbusy property I want to filter my ICollectionview and push it on UI. This Filter operation I have put in

IsBusy = true;
await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
    new System.Action(() =>
    {
      this.MyCollectionView.Filter = item =>
      {
        Iitem IdName = item as Iitem;
        return Regex.IsMatch(IdName.Name, SearchText, RegexOptions.IgnoreCase);
      };
    }));

Workaround: If I put Task.Delay(200).Wait(); after setting IsBusy, busy indicator will be displayed for some cases.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
Jay
  • 1

1 Answers1

0

Filter always runs on the UI thread, keeping it too busy to actually update the UI to reflect the IsBusy state.

In most cases, Filter functions should run fast enough that they don't need any special handling. However, if you're sure that you need to run a time-consuming filter, then the best way to do it is to split into two different collections: MyCollection and MyFilteredCollection. Then, any time MyCollection (or the filter) changes, do something like this:

IsBusy = true;
var items = MyCollection.ToList();
var filter = SearchText;
MyFilteredCollection = await Task.Run(() =>
    items.Where(x => Regex.IsMatch(x.Name, filter, RegexOptions.IgnoreCase)));
IsBusy = false;

(this assumes that IsBusy will prevent MyCollection and SearchText from changing).

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810