I'm working on a WPF application which has its business logic handled by a class library (without MVVM). Most properties of the business logic are dependency properties, which allows for an easy data binding to the WPF UI. 

I have a datagrid that displays a collection of items (dependency property of the class):`ObservableCollection<ItemEntry> EntryCollection`.

The goal is to asynchronously call the `ItemEntryUpdateAnalyzer.Analyze(ItemTemplate, Company, entry)` static method for every item in the EntryCollection as the processing takes a few seconds.

I started by doing the following :

```
    private async void AnalyzeButton_OnClick(object sender, RoutedEventArgs e)
    {
        List<Task> tasks = EntryCollection.Select(entry => Task.Run(() => AnalyzeItemEntries())).ToList();
        await Task.WhenAll(tasks);
    }

    private void AnalyzeItemEntries()
    {
        Log.Debug("Begin");
        Thread.Sleep(500);
        Log.Debug("End");
    }

```

That worked just fine, but adding the processing method throws an System.InvalidOperationException on a dependancy property of the ItemTemplate

```
    private void AnalyzeItemEntries(ItemEntry entry)
    {
        Log.Debug("Begin");
        ItemEntryUpdateAnalyzer.Analyze(ItemTemplate, Company, entry); //InvalidOperationException
        Log.Debug("End");
    }

```

This is due to the fact that the arguments of the Analyze method belong the the main UI thread. So I tried using the dispatcher to give the right context by doing the following :

```
    private void AnalyzeItemEntries(ItemEntry entry)
    {
        Log.Debug("Begin");
        /*tried with InvokeAsync as well*/
        Dispatcher?.BeginInvoke((Action) (() =>
        {
            ItemEntryUpdateAnalyzer.Analyze(ItemTemplate, Company, entry);
        }));
        Log.Debug("End");
    }

```

But that doesn't really helps as this locks the main thread. The problem is that the arguments are bound the the UI by dependency properties, normal properties don't seem to throw the exception. 

**EDIT:**

I tried to deep copy the ItemTemplate and ItemEntry to local variables using the DeepCloner NuGet (<https://github.com/force-net/DeepCloner>) :

```
    private async void AnalyzeButton_OnClick(object sender, RoutedEventArgs e)
    {
        Log.Debug($"==== Main thread ID {Thread.CurrentThread} ===");

        ItemTemplate localTemplate = ItemTemplate.DeepClone();
        ObservableCollection<ItemEntry> localEntryCollection = EntryCollection.DeepClone();
        foreach (ItemEntry entry in localEntryCollection)
        {
            await Task.Run(() => AnalyzeItemEntries(localTemplate, entry));
        }
    }

    private void AnalyzeItemEntries(ItemTemplate template, ItemEntry entry)
    {
        Log.Debug($"Begin {entry.ItemCode}");
        ItemEntryUpdateAnalyzer.Analyze(template, Company, entry);
        Log.Debug($"End {entry.ItemCode}");
    }

```

I'm still getting the same error. The problem seems to be only related to dependency properties as accessing `entry.ItemCode` (standard property) works and accessing `entry.Action` does not.