ExcelDNA WPF task pane and ReactiveUI RxApp.MainThreadScheduler

45 views
Skip to first unread message

Mark

unread,
Sep 3, 2022, 7:05:29 AM9/3/22
to Excel-DNA
Hi,

I'm building an Excel-DNA addin with a WPF task pane. The WPF control is hosted within an ElementHost, similar to this example. The UI code is also using ReactiveUI.

The user control has a viewmodel that makes a remote request, transforms the result into a datastructure for presentation, and binds the result to a view model. Before the bind I use `ObserveOn(RxApp.MainThreadScheduler)` to make sure the binding happens on the UI thread.

Here's some example code:

```csharp
_cleanUp = catalogService.CatalogNodes.Connect()
    .TransformToTree(node => node.OwnerId)
    .Transform(node => new CatalogNodeViewModel(node))
    .Do(x => Logger.Info($"thread {Thread.CurrentThread.ManagedThreadId} before observe on"))
    .ObserveOn(RxApp.MainThreadScheduler)
    .Do(x => Logger.Info($"thread {Thread.CurrentThread.ManagedThreadId} after observe on"))
    .Bind(out _catalogNodeViewModels)
    .DisposeMany()
    .Subscribe(
        x => Logger.Info($"next: {x}"),
        ex => Logger.Info($"error: {ex.Message}")
    );
```

The problem I'm having is that the `RxApp.MainThreadScheduler` doesn't appear to be the UI/Excel/Main thread in the Excel-DNA add in, so I run into `error: This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.`

```
Thread 1|View.MainControl|-------------------- Starting main control
Thread 6|CatalogViewModel|thread 6 before observe on
Thread 6|CatalogViewModel|thread 6 after observe on
Thread 7|CatalogViewModel|error: This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
```

Whereas with the control hosted with a WPF App, everything works as expected:

```
Thread 1|View.MainControl|-------------------- Starting main control
Thread 5|CatalogViewModel|thread 5 before observe on
Thread 1|CatalogViewModel|thread 1 after observe on
```

Does anyone know how I can make sure the `Bind` happens on the Excel main thread?

Thanks, Mark
Reply all
Reply to author
Forward
0 new messages