I have a problem when reading a String list, here is my code.
public partial class form : Form
{
public static List<String> errores = new List<String>();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e){
BackgroundWorker bw = sender as BackgroundWorker;
// Extract the argument.
string arg = (string)e.Argument;
// Start the time-consuming operation.
// e.Result =
if (tags.prog2(arg) == false)
{
//guardar en lista de no completadas
form.errores.Add("a");
}
//some code here
}
private void buscar()
{
//Some code here
foreach (string i in rutas)
{
backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.RunWorkerAsync(i);
}
foreach (string prime in form.errores)
{
MessageBox.Show(prime);
}
}
}
but there is no elements in the list, why its happening this?
Obviously I call both functions
Aclaration
function1 its a backgroundWorker, so its called too many times, I dont know if this is related to the error.
Seems
read()is called beforefunction1()call. As function1 is called byBackgroundWorkerit is quite possible.You should keep a flag to indicate whether
function1is called or not. If not called, you should take preventive action.For the reference, following code is running well (and producing
"aaa"output)