I have a query. I would like to use the backgroundworker in my application, but I have no idea how to do it. My application takes a long time to perform certain validations, while this is done, it is frozen. The idea I have is to place a CANCEL VALIDATION button. How could I do it ?. This is my code, here the whole process is done.
void Listar()
{
try
{
CN_Guias guias = new CN_Guias();
DateTime Hoy = DateTime.Today;
string fecha_actual = Hoy.ToString("dd/MM/yyyy");
string mes, año;
mes = txt_mes.Text;
año = txt_anno.Text;
if (txt_ruta.Text == "")
{
MessageBox.Show("Ingresar Ruta por favor.!!!", "Ingresar Datos", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (txt_mes.Text == "")
{
MessageBox.Show("Ingrese el Mes por favor.!!!", "Ingresar Datos", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (txt_anno.Text == "")
{
MessageBox.Show("Ingrese el Año por favor.!!!", "Ingresar Datos", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
DataTable dtImagenes =
guias.Listar_Guias_xMes_xAño(Convert.ToInt32(mes),
Convert.ToInt32(año));
int cantidad_imagen_db = dtImagenes.Rows.Count;
pgb_cargando.Visible = true;
pgb_cargando.Maximum = cantidad_imagen_db;
pgb_cargando.Step = 1;
pgb_cargando.Value = 0;
//INICIO FOR
btn_listar.Enabled = false;
txt_mes.Enabled = false;
txt_anno.Enabled = false;
foreach (DataRow row in dtImagenes.Rows)
{
string nom_imagen_db = row["NroGuia"].ToString().TrimEnd(' ');
string[] archivos = Directory.GetFiles(txt_ruta.Text, nom_imagen_db + ".*");
string[] archivos2 = Directory.GetFiles(txt_ruta.Text, nom_imagen_db + "_*");
if (pgb_cargando.Value <= cantidad_imagen_db)
{
pgb_cargando.PerformStep();
}
if (archivos.Length == 0 && archivos2.Length==0) {
string f_guia = row["FechaGuia"].ToString();
guias.InsertarGuiasValidadas(nom_imagen_db,
Convert.ToDateTime(f_guia),
DateTime.Now, "NO");
}
}
MessageBox.Show("Se realizo la validación correctamente");
btn_listar.Enabled = true;
txt_mes.Enabled = true;
txt_anno.Enabled = true;
Limpiar();
//FIN FOR
//btn_validar.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
I hope, you can help me. Thanks.