In my ViewModel I have this code:
Logs = new ObservableCollection<Log>();
Logs = Task.Factory.StartNew(() => mainModel.GetLogs()).Result;
wtih Log being a very simple class with a couple of public properties.
According to my understanding of Task class the mainModel function GetLogs() invoked this way should run on a separate thread and while it is fetching records from the database my UI should be responsive, however that is not what is happening, instead while records are being fetched from the data store my UI is blocked.
I was hoping someone could explain why… TIA.
EDIT: My understading of the Task class was incomplete, using ContinueWith method of the Task class will ensure the async execution as explained below in member replies…
This is because you call
Resultright after you started the asynchronous operation. The getter of theResultproperty blocks the execution of current thread until the task is completed.Update:
In order to get the result asynchronously, you need to call
ContinueWithand specify a function that will be called when the task is completed: