How to not freeze/lock the application when querying data via SQL Server and create a "progress bar"?

Asked

Viewed 183 times

0

The code below as an example brings me great information from the database. Every time I click on the "Search" option of my program, my application freezes/hangs for a while until the select "pull" everything. I would like to know the best way of consulting this data without blocking the programme. And of course, to make it easier for the user, I would like to put a progress bar.

    private void button1_Click(object sender, EventArgs e)
        {
                SqlCommand myCommand = new SqlCommand("SELECT CONVERT(datetime, SF2010.F2_EMISSAO) AS [DATA DE EMISSÃO], " +
          "SF2010.F2_DOC AS [NF], " +
          "SD2010.D2_PEDIDO AS [PEDIDO TUPAN], " +
          "SC5010.C5_PEDCLI AS [PEDIDO CLIENTE], " +
          "SA1010.A1_CGC AS [CNPJ], " +
          "SA1010.A1_NOME AS [CLIENTE], " +
          "SA1010.A1_MUN AS [MUNICÍPIO], " +
          "SF2010.F2_EST AS [ESTADO]," +
          "SA4010.A4_NOME AS [TRANSPORTADORA], " +
          "SF2010.F2_TPFRETE AS [FRETE], " +
          "SF2010.F2_VOLUME1 AS [VOLUME], " +
          "cast(SF2010.F2_PBRUTO AS DECIMAL(15,2)) AS [PESO BRUTO], " +
          //  "cast(sum( SD2010.D2_TOTAL )AS DECIMAL (15,2)) AS [TOTAL DA NOTA (R$)]" + //ACABA O SELECT, FROM ABAIXO
          "SF2010.F2_VALBRUT AS [TOTAL DA NOTA (R$)], " +
          "SF2010.F2_UDTAGEN as [DATA DE AGENDAMENTO]," +
"SF2010.F2_UHORAGE as [HORA DO AGENDAMENTO]," +
"SF2010.F2_UDTSOLI as [DATA DA SOLICIT. DE COLETA]," +
          "SF2010.F2_UNUMCOL as [NÚMERO DA COLETA]," +
"SF2010.F2_DTSAIEN as [DATA DE SAÍDA]," +
"SF2010.F2_DTENTRE as [DATA DE ENTREGA]" +
                    "FROM SA4010 " +
          "RIGHT JOIN(SA3010 AS VCAD RIGHT JOIN(SA3010 AS VFAT " +
          "RIGHT JOIN(SC5010 RIGHT JOIN(SB1010 RIGHT JOIN(SD2010 LEFT JOIN(SA1010 RIGHT JOIN SF2010 " +
          "ON(SA1010.A1_LOJA = SF2010.F2_LOJA) AND (SA1010.A1_COD = SF2010.F2_CLIENTE)) ON (SD2010.D2_FILIAL = SF2010.F2_FILIAL) " +
          "AND(SD2010.D2_SERIE = SF2010.F2_SERIE) AND(SD2010.D2_DOC = SF2010.F2_DOC)) ON SB1010.B1_COD = SD2010.D2_COD) " +
          "ON SC5010.C5_NUM = SD2010.D2_PEDIDO) ON VFAT.A3_COD = SC5010.C5_VEND1) ON VCAD.A3_COD = SA1010.A1_VEND) " +
          "ON SA4010.A4_COD = SF2010.F2_TRANSP WHERE(((SD2010.D2_FILIAL) = ([SF2010].[F2_FILIAL])) " +
          "AND((SC5010.C5_FILIAL) = ([SF2010].[F2_FILIAL]))) " +
          "AND SF2010.D_E_L_E_T_ <> '*' AND SD2010.D_E_L_E_T_ <> '*' AND SA1010.D_E_L_E_T_ <> '*' " +
          "AND SC5010.D_E_L_E_T_ <> '*' " + "AND F2_EMISSAO >='" + TextBoxAno.Text + TextBoxMes.Text + TextBoxDia.Text + "'" +
     "GROUP BY SF2010.F2_EMISSAO, SF2010.F2_DOC, SD2010.D2_PEDIDO, SC5010.C5_PEDCLI, SA1010.A1_CGC, " +
          "SA1010.A1_NOME, SA1010.A1_MUN, SF2010.F2_EST, SA4010.A4_NOME, SF2010.F2_TPFRETE, " +
          "SF2010.F2_VOLUME1, SF2010.F2_PBRUTO, SF2010.F2_VALBRUT, SF2010.F2_UDTSOLI, SF2010.F2_DTSAIEN, SF2010.F2_DTENTRE, SF2010.F2_UDTAGEN, SF2010.F2_UHORAGE, SF2010.F2_UNUMCOL     " + //ADICIONADO F2_VALBRUT " +
     "ORDER BY [DATA DE EMISSÃO] DESC, [NF] DESC;", Conexção.ConexaoConfig);
            myCommand.CommandTimeout = 240;
            SqlDataAdapter adp = new SqlDataAdapter(myCommand);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                //MessageBox.Show(dt.ToString());
                GradedeDados.DataSource = dt;
                GradedeDados.Sort(GradedeDados.Columns[0], ListSortDirection.Ascending);
  • 1

    Since it is an external request, an exact progress bar becomes unviable unless you use some form of Socket to validate it. I recommend making the "infinite" load circle that web applications normally use to represent this. To improve performance, you can do this asynchronous query, so your application can continue processing something that does not depend on this data while not receiving the return.

  • I can create a message by loading then. But in practice in this asynchronous way, what would the code look like?

  • If I understand your code, you make the query and play inside a datatable. It will not do much to asynchronous if it will depend on the data to come. If you really have something else to process, you need to use the Task library, for example.

  • @Gabrielcoletta if he does async at least his application does not freeze, then as Voce himself suggested does the load "infinite" until receiving the answer of his task and good.. : ) because there is no way to know how long is the load progress of its query,

  • @Thiagoloureiro Opa, I didn’t realize that it wasn’t web the application, so doing asynchronous helps yes.

  • Guys, much help, but I’m learning to develop still... I wanted to know what the code would look like on top of the one I sent

  • I have seen talk of this asymptote but I did not understand very well and I do not know apply such code to my

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.