I have a function that populates a combo box in my winform app. The operation takes a few seconds so I'd like to make it async.
Here is the function:
public static List<string> GetAllPostCodeRegions(string country)
{
using (var db = new PlaceDBContext())
{
var regions = db.PostCodes.Where(pc => pc.Country == country).OrderBy(pc => pc.Region).Select(pc => pc.Region).Distinct().ToList();
return regions;
}
}
I tried just adding in async keywords like below:
public async static Task<List<string>> GetAllPostCodeRegions(string country)
{
using (var db = new PlaceDBContext())
{
var regions = db.PostCodes.Where(pc => pc.Country == country).OrderBy(pc => pc.Region).Select(pc => pc.Region).Distinct().ToList();
return regions;
}
}
But this isn't awaiting anything and the UI still locks when I call:
var regions = await DataFunctions.GetAllPostCodeRegions("UK");
How do I make this operation async and stop UI locking up?
using System.Data.Entityas using the ctrl + period didn't suggest it for some reason.