Tuesday 24 May 2016

Call async task method from another class using wpf/C#

I'm trying to call an async Task method, from another class. The method is place within a service I have created.



Service:



public class GetRoomsService
{

public async Task> SearchForAvailableRooms(DateTime from, DateTime to)
{
ObservableCollection data = new ObservableCollection();
return data;
}
}


In my wpf viewModel where I need to make the call, I have done it like:




// Start a search with default values from searchModel
RoomsList = _getRoomsService.SearchForAvailableRooms(searchModel.From, searchModel.To);


Where _getRoomsService in an instants of the service class.
Now I need to call it when it's set to async Task



Have tried with:



RoomsList = Task.Run>(() => { return _getRoomsService.SearchForAvailableRooms(Search.To, Search.From); }).Result;   



This does not work, and gives me an "Cannot convert lambda expression to delegate type" error.



Hope someone can see what I'm doing wrong.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...