Rather than incurring the cost of thread management, let the task parallel library do this for you:
protected void SomeButton_OnClick(Object sender, EventArgs e)
{
// A new task is not necessarily a new thread!
Task.Factory.StartNew(() => PopulateSomeControl());
}
private async Task RequestDataAndPopulateTheControl()
{
Statusbar.Text = "Loading from DB...";
// this blocks only this method, not the UI thread.
var data = await RequestData();
// data is now available
foreach(var whatever in data.ToList())
SomeControl.Items.Add(whatever);
Statusbar.Text = "Ready.";
}
private async Task<DataTable> RequestData()
{
var db = new YourDbContext();
return db.SomeDbSet().ToDataTable();
}
The method RequestDataAndPopulateTheControl()
now becomes a readable union of your asynchronous threaded request for data as well as the callback to use the data. The UI thread is not blocked.