Click here to Skip to main content
15,798,592 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
well am working on an application that use a main Form , the main Form contain a panel in witch i load usercontrol , by cliking a button , so when i click button i launch a user control named Diag , well its working , however in the user control interface i have a combobox an other things like buttons,text box ...
i have an event in my user control diag ,
private void diag_Load(object sender, EventArgs e)

load_combobox() is a void that load data from sqldatabase to my combobox
well every thing is working but the ui is freezing for some seconds because a lot of items add to combobox
i want some help to run load_combobox () async in an other task
please if some one can help me

What I have tried:

Task backgroundDBTask = Task.Factory.StartNew(() =>
    SqlCommand cmd = new SqlCommand("afficher_List_Med", con);
    cmd.CommandType = CommandType.StoredProcedure;
    DataTable dt2 = new DataTable();

    foreach (DataRow row in dt2.Rows)
         Medico.Add(new Medic { Med = row[1].ToString(), idmed = (int)row[0] });
        if (row[3].ToString() != "")
            int i = dose.FindIndex(x => x == row[3].ToString());
            if (i < 0)


        if (row[4].ToString() != "")
            int i = form.FindIndex(x => x == row[4].ToString());
            if (i < 0)

        if (row[5].ToString() != "")
            int i = pose.FindIndex(x => x == row[5].ToString());
            if (i < 0)


backgroundDBTask.ContinueWith((t) =>
    comboBox_Medicament.DataSource = Medico;
    comboBox_Medicament.ValueMember = "idmed";
    comboBox_Medicament.DisplayMember = "Med";
    comboBox_Forme.DataSource = form;
    comboBox_posologie.DataSource = pose;
    comboBox_dosage.DataSource = dose;

here the code is separate on tow parts , fetch data from data base in list then
load data in combobox
so i want this run async in backgroud
Updated 29-Mar-21 2:27am
Richard MacCutchan 24-Mar-21 6:43am    
"i tried to use async task but failed , so any idea"
Please use the Improve question link above, and show the code that you have tried, and explain exactly what happens when you run it. Saying "but failed" tells us nothing.
Rouag Hichem 29-Mar-21 7:03am    
thx for your answer , well here is my code

Code moved to question.
CHill60 24-Mar-21 6:55am    
When you say "a lot of items" ... have you considered your poor users? Don't load huge amounts of data at once to UI controls - limit the number of items that are included. Even if the list is sorted and searchable no-one wants to hang around while loads of unnecessary data gets loaded.
But as Richard has pointed out, we at least need to see the code for load_combobox(); before we can help you at all
Rouag Hichem 29-Mar-21 7:04am    
as you asked me i posted my code to Richard

1 solution

Basically, you have a problem: you want to load a large amount of data into a UI control, without the UI freezing up.

And that's a problem, because only the UI thread can access UI controls, and running a long task on the UI thread means it can't do anything else so teh display never gets updated.

So do two things:
1) Move your combobox loading code into the Form.Shown event so the user can see something happened.
2) Rewrite your load code so it can use a second thread.
Create a BackgroundWorker[^] instance, as tell it to report progress. Add DoWork, ProgressChanged, and RunWorkerCompleted event handlers, and set the Cursor to the wait cursor.
Then start the worker running.
In the DoWork handler, fetch your data from the DB, and package it up in smallish groups - maybe 10 items per group - and report each as a progress event, passing the Group via the UserState property.
In the ProgressChanged handler, fetch the group from the UserState, cast it back to the group type, and add each item to your ComboBox.
In the RunWorkerCompleted handler, set your Cursor back to the default cursor.

Alternatively, read the data in the DoWork, then pass it back as a single object as a Progress and use that as the DataSource for the ComboBox if that's your preference.
Share this answer
Abdikader Mohamed 24-Mar-21 10:25am    
Student performance using linear Regression python source code
OriginalGriff 24-Mar-21 11:03am    
That is probably a sentence - just - but in the "Meaning" stakes it's completely lacking ...
Dave Kreskowiak 24-Mar-21 11:24am    
If you're looking for someone to write your code for you, you've come to the wrong site. Try
Rouag Hichem 29-Mar-21 8:18am    
sorry , am not looking for someone to code for me , i just need some help to solve a problem about UI freezing , as i mentioned up i wrote the code it work ,however i don't know how to make the processing run async
in my case need to load data to a combbox (more then one ) from my database ; but make the void fetch and load the data without freezing the user interface
i posted the code
Dave Kreskowiak 29-Mar-21 10:41am    
I wasn't replying to you, but to whoever Abdikader Mohamed is.

But, if you're loading thousands of items into a Combo, the UI is going to freeze or perform poorly, period. Controls are not meant to have thousands of items in them. Would you want to scroll through all those items to find the one you want?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900