0

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<ObservableCollection<Room>> SearchForAvailableRooms(DateTime from, DateTime to)
    {
        ObservableCollection<Room> data = new ObservableCollection<Room>();
        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<ICollection<Room>>(() => { 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.

4
  • 1
    If its not a duplicate (ie. you are calling from a method also marked as async) then its just a matter of doing var roomlist = await _getRoomsService.SearchForAvailableRooms(Search.To, Search.From);. Also its good practice to append Async to the name of your methods that are marked with the keyword async. So your method name would now be SearchForAvailableRoomsAsync Commented Apr 13, 2016 at 11:04
  • Have tried with await.. but it give me an "await can only be used within an async method" but it is async? or am I missing something? Commented Apr 13, 2016 at 11:10
  • It would help if you posted your method signature that you are wanting to call this from. Commented Apr 13, 2016 at 11:25
  • Found a way to solve this with "var result = task.GetAwaiter().GetResult();" Commented Apr 13, 2016 at 11:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.