1

Estoy actualizando un DataGrid cada 5 segundos, el problema es que a veces suele trabarse y necesito ejecutarlo en segundo plano. Para evitarlo, he visto que puedo hacerlo con BackgroundWorker pero no entiendo muy bien como va el rollo.

  bd.ejecutarConsulta("SELECT ID,Entrada,totalCajas,totalPeso,desechoCajas,desechoPeso,time from separacion WHERE ticket=0");
        DataTable dt = new DataTable();
        if (bd.myReader != null)
        {
            dt.Load(bd.myReader);
            dgvDatos.DataSource = dt;
            if (dgvDatos.RowCount > 0)
            {
                bd.cerrarConsulta();

                PrintDocument printDocument = new PrintDocument();
                printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt); //add an event handler that will do the printing

                foreach (DataGridViewRow row in dgvDatos.Rows)
                {
                    ID = row.Cells[0].Value.ToString();
                    ent = row.Cells[1].Value.ToString();
                    tcajas = row.Cells[2].Value.ToString();
                    tpeso = row.Cells[3].Value.ToString();
                    dcajas = row.Cells[4].Value.ToString();
                    dpeso = row.Cells[5].Value.ToString();
                    time = row.Cells[6].Value.ToString();

                    printDocument.Print();
                    bd.ejecutarConsulta("Update separacion set ticket = 1 where ID =" + row.Cells[0].Value.ToString());
                }
                dgvDatos.DataSource = null;
            }
        }
3
  • 2
    y donde esta el background worker? await y task son la solucion mas moderna
    – gbianchi
    el 28 mar. 2018 a las 22:50
  • msdn.microsoft.com/es-es/library/hh191443(v=vs.120).aspx aquí la explicación oficial
    – TianZi
    el 29 mar. 2018 a las 1:55
  • Necesitas poner mas código, desde dónde llamas al código que pusiste por ejemplo. NO utilices backgroundworker ya que te complicas y metes mas métodos que con async/await te los evitas.
    – Luis
    el 30 mar. 2018 a las 2:35

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.