Good...
I have the following situation:
I have two lists, the first of approximately 9,000 objects, and the second of 44,000 (this may vary from one run to the next).
Now, I have to find each item in list A in list B, set a couple of attributes, and then continue working with those in list A modified.
For this, I have two foreach nested in a new Thread and I intend to fill a ProgressBar while the process lasts (Threads is new to me in C#)
The problem is that while the process is running, I get an exception of the type:
System.Reflection.TargetInvocationException
Here the code:
private void btn_readTXT_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Thread backgroundThread = new Thread(
new ThreadStart(() =>
{
string url = file.FileName;
IBs = otequiste.ReadFile(url); //Lleno la lista desde el txt
foreach (cliente cliente in Clientes)
{
foreach (tequiste ib in IBs)
{
if (cliente.Cli_CUIT == ib.Cuit)
{
cliente.Actividad = ib.Cod_act;
cliente.Riesgo = ib.Riesgo;
cliente.Tipo = ib.Tipo;
progressBar1.BeginInvoke(
new Action(() =>
{
progressBar1.Value += 1;
}
));
break;
}
}
}
MessageBox.Show("Ready", "Ready ? ");
}));
backgroundThread.Start();
}
}
My doubts are the following:
a) What does this exception mean? (I don't know when it happens, the bar fills up to the middle after about 5 seconds, the visual editor leaves me in the Program.cs file in a line that says:
Application.Run(new Form1());
b) Taking into account that the number of objects in the lists varies from one execution to the next, how can I effectively fill the progress bar?
(that is, calculate 100% and know how much to increase the Value so that it does not go out of range or fall short)
Given the discussion we had in the chat , two problems were identified, one was performance , since by having a
foreach
nest you are practically doing a cross product, and the second was the value ofprogressBar1
, it was outside the allowed range.The solution to the performance section is to look up the value
cliente.Cli_CUIT
via Linq:I leave the line
progressBar1.Value += 1;
commented out, this is because the code has changed with the modifications you made and with which it is already working correctly. The problem was that the maximum value allowed for aProgressBar
is 100, so by increasing it by 1 with each iteration, there would come a time when it was out of that range, the solution was to divide the number of records processed at the time of each client iteration between the total number of customers.