Question

Is there any way how to do

ICollectionView.Refresh() 

or

CollectionViewSource.GetDefaultView(args.NewValue).Refresh();

in a separate thread?

I know I can use dispatcher, but this collection is binded to a ListView and it throws cross thread Exceptions.

The reason why I need a second thread is, that I have Control which displays a list of IMyItems. When filtering this Collection (by user text change input), I want to be able to display my animation that CollectionView is changing.

Was it helpful?

Solution

You can't!

All UI operations must happen on the user interface thread, and nearly every call inside of WPF's DispatcherObject (and all controls in that hierarchy) are regularly going to be calling CheckAccess().

You might want to consider using an ObservableCollection to help keep your data up-to-date, if you're doing processing in a background thread or BackgroundWorker.

OTHER TIPS

How about using the Dispatcher to do work with background priority?

Dispatcher.Invoke(DispatcherPriority.Background,
    () => { CollectionViewSource.GetDefaultView(args.NewValue).Refresh(); }
);

I hacked up a quick method to invoke actions on wpf dispatchable objects (all wpf controls inherit from DispatcherObject)

public static void InvokeWpf(DispatcherObject dispatchable, Action action, bool async)
{
    // DispatcherOperationCallback is optimized for wpf invoke calls
    DispatcherOperationCallback toDo = delegate{ action(); return null; };

    if (!dispatchable.CheckAccess())
    {
        if (async) 
            dispatchable.Dispatcher.BeginInvoke(toDo, null);
        else
            dispatchable.Dispatcher.Invoke(toDo, null);
    }
    else
    {
        toDo(null);
    }
}

Usage:

InvokeWpf(listView, 
       () => CollectionViewSource.GetDefaultView(listView).Refresh(), 
       false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top