Search code examples
wpfmultithreadingxamlmvvmdispatcher

Is Dispatcher not required in MVVM patern with WPF?


I am starting a new thread and trying to update UI elements through properties defined in my View Model and I am able to do it without any error, but if I try to update UI elements through code-behind, it throws the known UI access error("The calling thread cannot access this object because a different thread owns it."). First question would be ..Whats the difference between the two approaches ? Second question would be when I would use Disptacher in ViewModel ideally ?

Code Behind

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Thread th = new Thread(new ThreadStart(delegate()
            {
                textbox.Text = "Rajib";
            }
        ));

        th.Start();
    }

//inside XAML
<TextBox x:Name="textbox" Text="{Binding UserInput, Mode=TwoWay}" />

MVVM

public string UserInput
    {
        get { return _UserInput; }
        set { _UserInput = value; OnPropertyChanged("UserInput"); }
    }

//Called through a ICommand property on a button click public void ExecuteCommand(object obj) { InvokeCallThroughAnonymousDelegateThread(); }

private void InvokeCallThroughAnonymousDelegateThread()
    {
        ThreadStart start = delegate()
        {
            UserInput = "Calling from diff thread";
        };           
        new Thread(start).Start();
    }

Solution

  • Any attempt to update the UI must be done within the dispatcher thread. However, for property change events, WPF automatically dispatches for you when the event is raised from a background thread. You can read more about this on Bea Costa's (former WPF data binding PM) blog:

    http://bea.stollnitz.com/blog/?p=34

    They were going to do the same for INotifyCollectionChanged events but never got around to it in prior releases. For 4.5 they will now be synchronizing collection changed events automatically in addition to INotifyPropertyChanged.