I have a question. 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.
The class
BackgroundWorker
allows you to run an operation on asubproceso
dedicated standalone. Time-consuming operations such as downloads and database transactions can cause the user interface (UI) to appear as if it has stopped responding while they are running. If you wantIU
dynamic and are experiencing long delays associated with these operations, the classBackgroundWorker
provides a suitable solution.To run a slow operation in the background, create a
BackgroundWorker
and listen for events that report the progress of the operation and a signal when the operation completes. You can create theBackgroundWorker
programmatically or drag it onto the form from the Components tab of the Toolbox. If you create theBackgroundWorker
in the DesignerWindows Forms
, it will appear in the Component Tray and its properties will be displayed in the Properties window.To set up a background operation, add an event handler for the event
DoWork
. Call the time-consuming operation in this event handler. To start the operation, callRunWorkerAsync
. To receive notifications of progress updates, monitor the eventProgressChanged
. To be notified when the operation is complete, handle the eventRunWorkerCompleted
.You must place what you are going to do inside the function
DoWork
and startBackgroundWorker
it so that it works asynchronouslybackgroundWorker1.RunWorkerAsync();
:I leave you the source if you want more information .