0

Been reading up on the background worker, primarily the doWork method. It stated that this should have the calculations but should not be dependent on GUI code in anyway. This is my function:

void fillLiguanea()
{
    //  this.liguanea_LaneTableAdapter1.Fill(this.pharmaciesDataSet1.Liguanea_Lane);
    try
    {
        string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        string query = "SELECT * FROM dbo.Liguanea_Lane2";
        SqlCommand cmd = new SqlCommand(query, con);

        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string scode = dr.GetString(dr.GetOrdinal("code"));
            comboBox2.Items.Add(scode);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Within the above function there is a comboBox entitled: "comboBox2." My question is does that mean I couldn't call my overall above function in the doWork method just because of this comboBox controller?

Marc
  • 3,905
  • 4
  • 21
  • 37
Javy26
  • 375
  • 1
  • 7
  • 22
  • 1
    Fill a `List` and assign it to e.Result. Your RunWorkerCompleted event handler can then update the combobox. – Hans Passant Nov 07 '16 at 16:30
  • There are already lots of questions and answers on Stack Overflow addressing how to interact with UI objects from another thread, including the worker thread used by `BackgroundWorker`. See the marked duplicate for one such example. If you find yourself still having difficulty, after reading the large volume of available information, post a new question that includes a good [mcve] that shows clearly what you've tried, with a detailed explanation of what research you've already done and what _specifically_ you're still having trouble understanding. – Peter Duniho Nov 07 '16 at 17:11

2 Answers2

0

Backgroundworker is basically obsolete if you are using .Net 4.5 or later in favor of async/await. So if you are going to learn something new, I suggest you learn that.

If you are still going to use backgroundworker, you would raise the ProgressChanged event within your while loop and do you combobox manipulations in the implementation of that event handler so you have access to the UI thread.

Edit

Since there is really no reason to update the combobox as you go, I'd take Hans' advice.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

the basic idea is that you cannot make any gui calls or changes from the current background worker thread (doWork function), for the simple reason that it is not that thread but the main thread that created the graphic object. (you can't modify an object instanciated under a different thread)

That is why background worker class supports methods such as progress changed or completed, in which you can modify the UI.

be careful of invocation though, it is more secure to use invoke_required.

check google for articles on how to make safe cross thread operations

crazyghandi
  • 49
  • 1
  • 10