ProgressBar progress according to the data sent

1

I am trying to make my ProgressBar advance according to the data sent to the service. Everything works very well the problem is to let the user know the progress of the data sent using ProgressBar .

private void Enviar()
{
    OleDbDataReader read = MSAConnection.read(@"
                                            SELECT 
                                                Nombre
                                            FROM Persona");
    progressBar.Maximum = 100;
    int count = 0;
    int countPro = 0;

    for (int i = 0; i < total; i++)
    {
        while (read.Read())
        {
            string nombre = Convert.ToString(read["Nombre"]);

            WebRequest request = WebRequest.Create(@"http://localhost:7777/Services/Persona.svc/Persona/" + nombre);
            request.Credentials = CredentialCache.DefaultCredentials;
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            reader.Close();
            response.Close();

            count = i;
            progressBar.Value = countPro++;
       }
    }
    read.Close();
}
    
asked by Pikoh 14.03.2018 в 17:26
source

2 answers

0

Try doing this:

progressBar.Maximum = 100;
progressBar.Minimum = 0;
progressBar.Step = 1;
int porciento = 0;

 //....
 //Tu otro código....

 countPro++;
 porciento = Convert.ToInt32(( ( (double)countPro/ (double)total ) * 100.00 ));
 if (countPro >= total)
     progressBar.Value = progressBar.Maximum;
 else
     progressBar.Value = porciento;
  

Another thing, as I see that you have a OleDbDataReader read The data readers are really just forwarding readers, there are no properties like Count() to return the count of the result rows. you could substitute the total in this line: int porciento = Convert.ToInt32(( ( (double)countPro/ (double)total ) * 100.00 )); for the total records of the query totalfilas and at the end of the cycle While of read return the countPro = 0 that when you again go through the cycle for start again from zero the ProgressBar for each record of the cycle for while you walk the While

You can change the query to:

OleDbDataReader read = MSAConnection.read(@"
                                            SELECT 
                                                Nombre, count(*) as totalFilas 
                                            FROM Persona");

  //......
  //.......
  while (read.Read())
  {
        string nombre = Convert.ToString(read["Nombre"]);
        int totalFilas= Convert.ToInt32(read["totalFilas"]);
        //....
        //.....
        countPro++;
        porciento = Convert.ToInt32(( ( (double)countPro/ (double)totalFilas ) * 100.00 ));
        if (countPro >= totalFilas)
            progressBar.Value = progressBar.Maximum;
        else
            progressBar.Value = porciento;
 } 
 countPro = 0;

That would allow you to retrieve the row count. Or use something like this:

int totalfilas = 0; 
SqlCommand cmd = new SqlCommand("SELECT count(*) FROM Persona", conn);
try
{
    conn.Open();
    totalfilas = (Int32)cmd.ExecuteScalar();
}
    
answered by 14.03.2018 в 18:06
0

Remember that when you perform processes such as database reading, the user interface may be blocked and your progress bar may not be updated.

You can obtain data using a backgroundworker.

Declare a variable like this in your form:

BackgroundWorker  bgw = New BackgroundWorker();

In the constructor you initialize the progressbar values and assign the backgroundworker events:

public TuClase()
{
    progressBar.Maximum = 100;
    bgw.DoWork += bgw_DoWork;
    bgw.ProgressChanged += bgw_ProgressChanged;
}

You create a sub method like this:

private void bgw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
     // aquí pones el codigo que carga los datos
    OleDbDataReader read = MSAConnection.read(@"
                                        SELECT 
                                            Nombre
                                        FROM Persona");
    // etc...

     //  y para actualizar el progress bar 
    bgw.ReportProgress(porcentaje);
    // donde porcentaje es una variable donde calculas el porcentaje

}

Then you add another method where you directly update the progress bar:

private void bgw_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) 
{
    // aquí actualizas el progressbar, y cualquier otro control que tengas que actualizar
    progressBar.Value = e.ProgressPercentage;
}

When you want to start the data download process you use:

private void BotonDeCarga_Click(object sender, EventArgs e) 
{
    bgw.RunWorkerAsync()
}

Where Charge Button can be the button that starts loading data.

I did it by heart, I'm not 100% sure that it works as it is because I wrote it by heart, but in theory it should work.

    
answered by 18.03.2018 в 21:48