This is the first time I use a Parallel.ForEach loop and hoping I can get some help with it. So basically within the Parallel.ForEach loop, I'm doing unit coversions but in the example code below, I am just getting the To / From units to be concatenated which would then be used to select the correct conversion formula. Below is an example. There's more code than what I'm showing but I don't think it's needed to get the help I'm looking for.
The code works great when the if (tmp_cbox.Name == "P1_Units_CBox") block is true but when the else if (tmp_cbox.Name == "Viscosity_Units_CBox") in red text below is true, I get the following error:
Message=Cross-thread operation not valid: Control 'Viscosity_Type_CBox' accessed from a thread other than the thread it was created on.
To give a little more detail, in the else-if block that's causing the error, there are two (2) comboboxes that are looked at. Viscosity_Type_CBox combobox allows the user to choose whether Dynamic or Kinematic viscosity is to be used. The Viscosity_Units_CBox combobox is then updated to include the units based on the Viscosity_Type_CBox selection.
I would appreciate any help you could provide in getting this to work properly and getting an understanding to the cause of the eror. I'm aware of BackgroundWorker but not sure how to use it. If there are better ways than BackgroundWorker, please suggest. Thank you!!
The code works great when the if (tmp_cbox.Name == "P1_Units_CBox") block is true but when the else if (tmp_cbox.Name == "Viscosity_Units_CBox") in red text below is true, I get the following error:
Message=Cross-thread operation not valid: Control 'Viscosity_Type_CBox' accessed from a thread other than the thread it was created on.
To give a little more detail, in the else-if block that's causing the error, there are two (2) comboboxes that are looked at. Viscosity_Type_CBox combobox allows the user to choose whether Dynamic or Kinematic viscosity is to be used. The Viscosity_Units_CBox combobox is then updated to include the units based on the Viscosity_Type_CBox selection.
I would appreciate any help you could provide in getting this to work properly and getting an understanding to the cause of the eror. I'm aware of BackgroundWorker but not sure how to use it. If there are better ways than BackgroundWorker, please suggest. Thank you!!
C#:
Parallel.ForEach(unit_list, unit =>
// Call to Get_From_To_Unit_Concat with return of the To / From units concatenated
tmp_concat_units = Get_From_To_Unit_Concat(pre_selected_unit, unit, unit_list, tmp_cbo);
});
//****************************************************************
private string Get_From_To_Unit_Concat(string temp_curr_unit, string unit, List<string> tmp_umits_list, ComboBox tmp_cbox)
{
string pre_selected_unit = string.Empty;
string post_selected_unit = string.Empty;
List<string> get_cbo_list = new List<string>();
if (tmp_cbox.Name == "P1_Units_CBox")
{
get_cbo_list = new List<string> { "psig", "psia", "barg", "bara", "kg_cm2_g", "kg_cm2_a", "kPa_g", "kPa_a", "in_wc_g", "mm_wc_g", "Pa_g", "Pa_a", "MPa_g", "MPa_a" };
}
[COLOR=rgb(184, 49, 47)]else if (tmp_cbox.Name == "Viscosity_Units_CBox")
{
if (Viscosity_Type_CBox.SelectedIndex == 0)
get_cbo_list = new List<string> { "cP", "mPa_s", "lbm_ft_s", "kg_m_s", "Pa_s" };
else if (Viscosity_Type_CBox.SelectedIndex == 1)
get_cbo_list = new List<string> { "cSt", "SSU", "mm2_s", "ft2_s" };
}[/COLOR]
// code continues........