1
good morning!
I set up a small study project where I collect data from a site and game for a Datagridview using Selenium. I have 3 classes
Robot.Cs -> Where I have the methods of navigation and data collection.
//NAVEGAR PARA O SITE(URL)
public void GoToUrl(string Url)
{
RoboDriver.Navigate().GoToUrl(Url);
}
//PROCURAR POR XPATH LER E RETORNAR VALOR
public string ReadByXPath(string XPath, string attribute)
{
string SetRead;
Wait.Until(d => RoboDriver.FindElementByXPath(XPath));
SetRead = RoboDriver.FindElementByXPath(XPath).GetAttribute(attribute);
return SetRead;
}
Robotnavigation.Cs -> Responsible for navigation.
//METODO PARA BUSCAR E PREENCHER AS COTAÇÕES DO DATAGRIDVIEW
public void GetQuotes(DataGridView dataGridView, ProgressBar progressBar)
{
//ZERAR BARRA DE PROGRESSO
progressBar.Value = 0;
//DEFININDO VALOR DA BARRA DE PROGRESSO
progressBar.Maximum = dataGridView.RowCount;
//PERCORRER REGISTROS DO DATAGRIDVIEW
foreach (DataGridViewRow row in dataGridView.Rows)
{
//TRAVAR DATAGRIDVIEW
dataGridView.AllowUserToAddRows = false;
string codActive = row.Cells[0].Value.ToString();
//MONTAR URL DE NAVEGAÇÃO COM CÓDIGO OBTIDO
string url = "https://www.fundamentus.com.br/detalhes.php?papel=" + codActive;
//NAVEGAR PARA SITE (URL)
GoToUrl(url);
//PREENCHER COLUNA DO DATAGRIDVIEW COM A COTAÇÃO OBTIDA NO SITE
row.Cells[1].Value = ReadByXPath("/html/body/div[1]/div[2]/table[1]/tbody/tr[1]/td[4]/span", "innerHTML");
//INCREMENTAR BARRA DE PROGRESSO
progressBar.PerformStep();
}
//FECHAR ROBO
RobotQuit();
//HABILITAR DATAGRIDVIEW
dataGridView.AllowUserToAddRows = true;
}
}
Form1.Cs -> Where I run navigation via a button.
private void bntPlay_Click(object sender, EventArgs e)
{
RobotNavigation robotNavigation = new RobotNavigation();
robotNavigation.GetQuotes(dgvMain, pBar);
}
I would like a help to make this project asynchronous so that the form and components do not lock while the robot navigates, collects data and works with the components passed via parameter.
You must implement a thread that runs the logic of data collection, so the application thread will not be locked until the end of the operation. Have a look here https://docs.microsoft.com/pt-br/dotnet/api/system.threading.threadpool.queueuserworkitem?view=netframework-3.5
– Lodi