MultiThreading with searchbox

Asked by At

I have a listbox with list of persons (first name ,last name , addresses...) and a searchbox (TextBox with TextChaned event the thing is i'm running a search throw database and it take a long time and the UI freeze for seconds,how can I make it responsive ?

1 Answers

Community On

Assuming that you are working with DataTables, here is what you can do:

private async void btnSearch_Click(object sender, EventArgs e) // async is important
    DataTable dt = await Task.Run(() => // await is important (avoids the UI freeze)
        return GetData(); // Fetch your data from DB
    // Fill your listbox with the data in dt