ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

3-14159265358979323846264

Thanks to @GlennWatson for pointing out that I needed to add a reference to the Nuget Package ReactiveUI.WPF, in addition to the ReactiveUI package.

I have a ReactiveObject view model, within which I would like to use an OpenFileDialog to set the value of one of my view model properties (PdfFilePath). Everything I have tried results in a The calling thread cannot access this object because a different thread owns it error.

I realise that the code below is not MVVM compliant because I am using code that 'explicitly references the type of/instantiates the view' in the view model, but I'm just looking for a minimal example that works so I can work backwards, splitting the view and view model code apart, and ultimately passing in a service to my view model that handles the whole 'selecting a file path' part.

public class ImportPdfViewModel : ReactiveObject
{
    public ImportPdfViewModel()
    {
        SelectFilePathCommand = ReactiveCommand.Create(() =>
        {
            OpenFileDialog ofd = new OpenFileDialog() { };
            //
            if (ofd.ShowDialog() == DialogResult.OK)
                PdfFilePath = ofd.FileName;
        });
    }

    private string _PdfFilePath;
    public string PdfFilePath
    {
        get => _PdfFilePath;
        set => this.RaiseAndSetIfChanged(ref _PdfFilePath, value);
    }

    public ReactiveCommand SelectFilePathCommand { get; set; }
}

As i mentioned, I have tried lots of different options, including injecting a service into my view model, but no matter where I instantiate the OpenFileDialog (eg in the main view), I always end up with the same error.

I've also googled the hell out of "ReactiveUI" and "OpenFileDialog", but none of the code I find seems to be up to date (eg using ReactiveCommand<Unit, Unit>), nor consistent with any other example! Thanks.


UPDATE

Thanks to @GlennWatson for pointing out that I needed to add a reference to the Nuget Package ReactiveUI.WPF, in addition to the ReactiveUI package.

As soon as I added it, the code worked!

The code now looks like this, which I believe is MVVM compliant, uses dependency injection, and uses the latest features/best practices of ReactiveUI (though I'm obviously open to criticism!):

ImportPdf

public class ImportPdfViewModel : ReactiveObject
{
    public ImportPdfViewModel(IIOService openFileDialogService)
    {
        SelectFilePathCommand = ReactiveCommand
            .Create(() => openFileDialogService.OpenFileDialog(@"C:\Default\Path\To\File"));
        SelectFilePathCommand.Subscribe((pdfFilePath) => { PdfFilePath = pdfFilePath; });
    }

    private string _PdfFilePath;
    public string PdfFilePath
    {
        get => _PdfFilePath;
        set => this.RaiseAndSetIfChanged(ref _PdfFilePath, value);
    }

    public ReactiveCommand<Unit, String> SelectFilePathCommand { get; set; }
}

IIOService

public interface IIOService
{
    string OpenFileDialog(string defaultPath);
}

OpenFileDialogService

public class OpenFileDialogService : IIOService
{
    public string OpenFileDialog(string defaultPath)
    {
        OpenFileDialog ofd = new OpenFileDialog() { FileName = defaultPath };
        //
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            return ofd.FileName;
        }
        else
        {
            return null;
        }
    }
}

UPDATE

I've also had the error below caused by the same missing package ... This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread

Glenn Watson

If running on a WPF or WinForms platform you need to make sure you include a nuget reference to ReactiveUI.WPF or ReactiveUI.Winforms.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

the calling thread cannot access this object because a different thread owns it wpf

From Dev

The Calling thread cannot access this object because a different thread owns it,WPF

From Dev

WPF: The calling thread cannot access this object because a different thread owns it

From

The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it (with Dispatcher)

From Dev

"The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

From Dev

Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

From Dev

The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

From Dev

The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

From Dev

Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

From Dev

The thread cannot access an object because a different thread owns it when using wpf

From Dev

c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

From Dev

Famous the calling thread cannot access this object because a different issue

From Dev

Different thread owns it in WPF

From Dev

Calling thread cannot access object due to separate thread ownership

From Dev

"calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

From Dev

WPF error 'calling thread cannot access this object' trying to play sounds in c# class

From Dev

Calling thread cannot access object when it's created from the same thread

From Dev

"The subprocess making the call can not access this object because the owner is another thread" Exception ASYNC/AWAIT WPF C#

From Dev

WPF Access window created on different UI thread

From Dev

The calling thread cannot access this object even after adding Dispatcher.Invoke

From Dev

Why is IllegalMonitorStateException thrown despite the thread calling notifyAll owns the monitor?

From Dev

Redshift - user "xyz" cannot be dropped because the user owns some object

From Dev

Updating image via Dispatcher still gives "different thread owns it" error

From Java

Calling into a saved java object via JNI from a different thread

From Dev

The calling thread must be STA, because many UI components require this, happening in main function in WPF

From Dev

InvalidOperationException, the calling thread should be STA because

From Dev

Pass object to thread (access object outside thread)

From Dev

Calling a Method in a running Thread Object

Related Related

  1. 1

    the calling thread cannot access this object because a different thread owns it wpf

  2. 2

    The Calling thread cannot access this object because a different thread owns it,WPF

  3. 3

    WPF: The calling thread cannot access this object because a different thread owns it

  4. 4

    The calling thread cannot access this object because a different thread owns it

  5. 5

    The calling thread cannot access this object because a different thread owns it (with Dispatcher)

  6. 6

    "The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

  7. 7

    Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

  8. 8

    The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

  9. 9

    The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

  10. 10

    Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

  11. 11

    The thread cannot access an object because a different thread owns it when using wpf

  12. 12

    c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

  13. 13

    Famous the calling thread cannot access this object because a different issue

  14. 14

    Different thread owns it in WPF

  15. 15

    Calling thread cannot access object due to separate thread ownership

  16. 16

    "calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

  17. 17

    WPF error 'calling thread cannot access this object' trying to play sounds in c# class

  18. 18

    Calling thread cannot access object when it's created from the same thread

  19. 19

    "The subprocess making the call can not access this object because the owner is another thread" Exception ASYNC/AWAIT WPF C#

  20. 20

    WPF Access window created on different UI thread

  21. 21

    The calling thread cannot access this object even after adding Dispatcher.Invoke

  22. 22

    Why is IllegalMonitorStateException thrown despite the thread calling notifyAll owns the monitor?

  23. 23

    Redshift - user "xyz" cannot be dropped because the user owns some object

  24. 24

    Updating image via Dispatcher still gives "different thread owns it" error

  25. 25

    Calling into a saved java object via JNI from a different thread

  26. 26

    The calling thread must be STA, because many UI components require this, happening in main function in WPF

  27. 27

    InvalidOperationException, the calling thread should be STA because

  28. 28

    Pass object to thread (access object outside thread)

  29. 29

    Calling a Method in a running Thread Object

HotTag

Archive