1

I have a generic List which contains almost 1100 elements. Almost 10 of these elements contain 1000 elements each (of same type). The elements are bound to UI DataGrid. Itrating this list takes long time 5-6 seconds (because the properties I manipulate in iterations are databound to DataGrid properties).

Here is iteration code:

Parallel.ForEach(this.AllParameters, par =>
{
  foreach (Parameter subPar in par.WrappedSubParameters)
  {
    subPar.IsSelected = false;
  }
  par.IsSelected = false;
});

The code snippet in Xaml looks like:

  <DataGrid.RowStyle>
     <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWay}" />

As in my previous question I was suggested to use Parallel iteration, but it hangs the UI and never returns back. How can I suspend the UI before doing the iterations in MVVM. Am I doing the code in right way? Please suggesst. Thanks

Community
  • 1
  • 1
Irfan
  • 2,713
  • 3
  • 29
  • 43

1 Answers1

2

You can find your answer here: link Basically the thing is, that you cant call your Parallel.ForEach on the UI thread. If you are not sure about current thread, then you can use method like this:

    public static bool CheckIsRunningOnUIThread()
    {
        if (Application.Current == null) return false;
        var dispatcher = Application.Current.Dispatcher;
        if (dispatcher==null) return false;
        return dispatcher.CheckAccess();
    }
Community
  • 1
  • 1
Steven
  • 99
  • 1
  • 4