I have a method seleniumCode()
that uses the 'chromedriver' of Selenium to do some things. this method is invoked by clicking a button on de GUI, which uses the main thread of the execution. When this method is running, the GUI is 'blocked' 'cause the main thread is collapsed by the seleniumCode(). To make this work right, I called seleniumCode() like this:
Thread th = new Thread(new ThreadStart(seleniumCode));
th.Start();
Here's the problem: I have some calls to 'writeLbx("each string")' inside seleniumCode(). And I can't write on the ListBox 'cause it's from another thread. So, after 2 weeks of brain-crashing, I arrived here.
I picked parts of some of your codes to make this:
private void writeLbx(string s)
{
switch (s)
{
case "Empezando_Tracking": lbxProcess.Items.Add("xxx");
break;
case "Mi Cuenta": lbxProcess.Items.Add("xxx");
break;
case "Email_Pass": lbxProcess.Items.Add("xxx");
break;
case "Iniciar sesión": lbxProcess.Items.Add("xxx");
break;
case "Procesando_Intento": lbxProcess.Items.Add("xxx");
break;
case "Precio_OK": lbxProcess.Items.Add("xxx");
break;
case "Poner_Cantidad": lbxProcess.Items.Add("xxx");
break;
}
lbxProcess.Update();
lbxProcess.TopIndex = lbxProcess.Items.Count - 1;
}
... into this:
private void writeLbx(string s)
{
this.Invoke(new Action(() =>
{
switch (s)
{
case "Empezando_Tracking": lbxProcess.Items.Add("xxx");
break;
case "Mi Cuenta": lbxProcess.Items.Add("xxx");
break;
case "Email_Pass": lbxProcess.Items.Add("xxx");
break;
case "Iniciar sesión": lbxProcess.Items.Add("xxx");
break;
case "Procesando_Intento": lbxProcess.Items.Add("xxx");
break;
case "Precio_OK": lbxProcess.Items.Add("xxx");
break;
case "Poner_Cantidad": lbxProcess.Items.Add("xxx");
break;
}
lbxProcess.Update();
lbxProcess.TopIndex = lbxProcess.Items.Count - 1;
}));
}
And that simply change makes my code work just like I want.