Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Please, I have created a winforms in C# and I want to use backgroundWorker to retrieve data from the database. This code works without using backgroundWorker and it makes the winForms slow once I tried to put the code under Form_Load.

Please any help would be appreciated.....

What I have tried:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {      
            try
            {
                con.Open();
                cmd = new MySqlCommand(@"SELECT CONCAT(fname,' ',lname,' ',oname) as name, phoneno FROM dbchurch.member WHERE status = 1;", con);
                reader = cmd.ExecuteReader();
                AutoCompleteStringCollection list_of_member = new AutoCompleteStringCollection();
                while (reader.Read())
                {
                    list_of_member.Add(string.Format("{0}", reader["name"]));
                    list_of_member.Add(string.Format("{0}", reader["phoneno"]));
                }
                backgroundWorker1.ReportProgress(0, list_of_member);
                reader.Close();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error occurred {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                reader.Dispose();
                con.Dispose();
            }
        }


private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            AutoCompleteStringCollection list_of_member = (AutoCompleteStringCollection)e.UserState;
            txtsearch.AutoCompleteCustomSource.AddRange(e.UserState);
        }
Posted
Updated 18-Feb-22 4:10am

1 solution

First off, I wouldn't use Progress reporting for that - I'd use that to add each item as it was processed, not the whole lot at once.
For a "single action" I'd handle the RunWorkerCompleted Event[^] instead as it's more obvious when is happening.

But your progress handling code doesn't help you at all:
AutoCompleteStringCollection list_of_member = (AutoCompleteStringCollection)e.UserState;
txtsearch.AutoCompleteCustomSource.AddRange(e.UserState);
Why cast the UserState to a useful type if all you do is ignore it and use the original object variable to set the range?
Since there isn't an AddRange overload that takes an object parameter, that probably doesn't even compile ...
 
Share this answer
 
Comments
Member 12887106 18-Feb-22 13:12pm    
I have an issue with the compile at txtsearch.AutoCompleteCustomSource.AddRange(e.UserState);. Can you assist with a code
OriginalGriff 18-Feb-22 14:17pm    
What issue? Do you think I can see your screen? :laugh:

This may help you: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]

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