dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

Convert method to return a task

How can I turn the below method into the right format for returning a task items. The method stub requires a task to be used. This is my first time into async methods I am using it for the windows phone 8 like below:

private System.Threading.Tasks.Task listView_PullToRefreshRequested(object sender, EventArgs e)
{
      Populatelist();
}

public async void Populatelist()
{
        try
        {
            curoListsDal _db = new curoListsDal();
            cLists = await _db.GetListsAync();
            listView.ItemsSource = cLists;
        }
        catch (Exception ex)
        {
        }
}

Upvotes: 1

Views: 667

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

The right format would be to return a Task instead of void in PopulateListAsync and await on that inside your event handler:

private async void PullToRefreshRequestAsync(object sender, EventArgs e)
{
    await PopulateListAsync();
}

public async Task PopulateListAsync()
{
    curoListsDal db = new curoListsDal();
    listView.ItemsSource = await db.GetListsAsync();
}

Side note: don't swallow exceptions.

Upvotes: 3

Related Questions