6
votes

My WPF application is structured using the MVVM pattern. The ViewModels will communicate asynchronously with a server, and when the requested data is returned a callback in the ViewModel is triggered, and it will do something with this data. This will run on a thread which is not the UI Thread. Sometimes these callbacks involve work that needs to be done on the UI thread, so I need the Dispatcher. This might be things such as:

  • Adding data to an ObservableCollection
  • Trigger Prism commands that will set something to be displayed in the GUI
  • Creating WPF objects of some kind.

I try to avoid the latter, but the two first points here I find to be reasonable things for ViewModels to do. So; is it okay to have ViewModels hold the Dispatcher to be able to Invoke commands for the UI thread? Or is this considered bad practice? And why?

4
I have had to do the same in controllers - the controller subscribes to the Load event of the view it creates, and at that point grabs a reference to the dispatcher of the view. This is especially useful for executing delegates that have been passed around.slugster
Thx for the tip! I'm using an IoC container, and the IoC container is created in App.xaml.cs. I'm assuming this is run in the UI thread, so the plan is to fetch the current dispatcher at the point the IoC container is created and add it to the container. Remains to see if this is successful.stiank81
Works perfectly. Or you can simply use Dispatcher.CurrentDispatcher at any parh you know is being run by the UI thread. Like e.g. the ViewModel constructors - if these are being constructed in the UI thread.stiank81

4 Answers

3
votes

Since an ObservableCollection must be updated on the thread it belongs to (assuming a GUI app), and ObservableCollections should be part of the ViewModel, then there's a clear case for the ViewModel having a Dispatcher.

I can't see it being part of the Model.

2
votes

Ideally, a ViewModel should be totally independent from the UI technology used. We should theoretically be able to reuse it for Windows Forms (if we pimp up the Windows Forms controls a little bit to support better binding), for Web pages (I envision some kind of fancy mechanism here that would compile the ViewModel also into Javascript), and for any future technologies. Not all of these technologies will use the Dispatcher model.

That said, i consider it as a pragmatic compromise to include the Dispatcher in the ViewModel nowadays. In my ViewModel base class, I check for the current Dispatcher:

    protected override void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (Deployment.Current.Dispatcher == null || Deployment.Current.Dispatcher.CheckAccess())
        {
            base.OnPropertyChanged(sender, e);
        }
        else
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => base.OnPropertyChanged(sender, e));
        }
    }

I still have the dependency on System.Windows of course, but oh well. :->

1
votes

I agree with kyoryu and I would like to note that it only creates a dependency on the ServiceModel lobrary (which you already have) and not on the View itself, so there is very little to object against this construction.

I was trying out a few things with WPF, a simple VM and threads yesterday and came to the conclusion that I absolutely needed to pass the Dispatcher to the VM.

Also see Using WPF UI thread should always ensure STA apartment mode, right?

0
votes

You should consider using AsyncOperation instead.