hi what is the easiest way to implement asynch operations on WPF and MVVM, lets say if user if user hits enter when on a field i want to launch a command and then return back while a thread will do some search operations and then come back and update the properties so notification can update the bindings.
thanks!
How about a BackgroundWorker instance to call your command on the VM ?
Update: Scratch the above suggestion.. There's an online video on MVVM by Jason Dolinger.. I recommend you take a look at that. It's a cleaner way where the view is thin/ does not hold any threading code.
To summarize:
_dispatcher.BeginInvoke( () => _results.AddRange( entries) )
so that the UI is updated correctly.Rob Eisenberg showed a really clean implementation of running async operations in MVVM during his MIX10 talk. He has posted the source code on his blog.
The basic idea is that you implement the command as returning an IEnumerable and use the yield keyword to return the results. Here is a snippet of code from his talk, which does a search as a background task:
Hope that helps.
In Shawn Wildermuth's MSDN Article he did something like this: check out the article here: http://msdn.microsoft.com/en-us/magazine/dd458800.aspx
and his more recent blog post here: http://wildermuth.com/2009/12/15/Architecting_Silverlight_4_with_RIA_Services_MEF_and_MVVM_-_Part_1
with an implementation like this: