Updating a property in the viewModel

66 Views Asked by At

I've a xaml file having a windows datagrid. Its Itemsource is a property in the associated ViewModel.

In my ViewModel class of xaml(i.e. client-side), I'm invoking a method M on the server using a messagging API. (The server is implemented using a .net windows service).

This method M internally uses .Net's Enqueue() and TPL (Task Parallel Library) to execute the method and return the results to the client(using messagging API).

I need to programmatically update a property in my ViewModel as soon as this method gets completed on the server. How do I achieve this please?

(The problem is that client makes a request to the server on a UI thread.The server runs asynchronously on a separate thread.)

Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

For scalar properties you don't need to do anything. When you call "OnPropertyChanged" the call is already marshalled to the UI thread. However, unfortunately some types like ObservableCollections are not thread safe. So the alternatives are to use a thread safe collection something like this, or to handle the marshalling yourself. Essentially use the Dispatcher to Invoke some code to the UI thread. Here is a simple example:

App.Current.Dispatcher.Invoke(new Action(()=>
{ 
    //The code I want to run on the UI thread.
}));