Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use progressbar in my form. I add "Application.DoEvents()" so that the progress bar fires when I click the button. However, in this case, I get the "index out of range" error. What do you think about how we can fix the error?

What I have tried:

C#
System.Threading.Thread.Sleep(10);

    this.backgroundWorker1.RunWorkerAsync();
    backgroundWorker1.WorkerReportsProgress = true;
    Cursor.Current = Cursors.WaitCursor;
    progressBar1.Show();
    label1.Show();
    pictureBox1.Show();
    progressBar1.Value = 0;

I want to make a percentage indicator as much as the selected data. I partially achieved this. However, when I do not place the code “Application.DoEvents()”, the progressbar percentage does not appear. When I put it in, it gives the error ” Index out of range”.
C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
    Application.DoEvents();
    progressBar1.PerformStep();

    var val0 = dataGridView1.Rows[i].Cells[0].Value.ToString(); 
        var val1 = dataGridView1.Rows[i].Cells[1].Value.ToString(); 
        var val2 = dataGridView1.Rows[i].Cells[2].Value.ToString(); 
        var val3 = dataGridView1.Rows[i].Cells[3].Value.ToString(); 
        var val4 = dataGridView1.Rows[i].Cells[4].Value.ToString(); /
        var val5 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[5].Value.ToString()); 
        var val6 = dataGridView1.Rows[i].Cells[6].Value.ToString();  
        var val7 = dataGridView1.Rows[i].Cells[7].Value.ToString();  
        var val8 = dataGridView1.Rows[i].Cells[8].Value.ToString();  


These are some parts of my code:
C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    if (dataGridView1.Rows.Count > 100)
    {
        int total = 500; //some number (this is your variable to change)!!

        for (int i = 0; i <= total; i++) //some number (total)
        {
            System.Threading.Thread.Sleep(10);
            int percents = (i * 100) / total;
            backgroundWorker1.ReportProgress(percents, i);
            Application.DoEvents();
        }
    }
    else if (dataGridView1.Rows.Count < 100)
    {
        int total = 100;

        for (int i = 0; i <= total; i++) //some number (total)
        {
            System.Threading.Thread.Sleep(5);
            int percents = (i * 100) / total;
            backgroundWorker1.ReportProgress(percents, i);
            Application.DoEvents();
        }
    }
}
private void backgroundWorker1_ProgressChanged
            (object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    label1.Text = String.Format("İşlem yüzdesi: {0} %", e.ProgressPercentage);
}

private void backgroundWorker1_RunWorkerCompleted
          (object sender, RunWorkerCompletedEventArgs e)
{
    progressBar1.Visible = false;
    label1.Visible = false;
    Cursor.Current = Cursors.Arrow;
    GC.WaitForPendingFinalizers();
    GC.Collect();
    Listele();
    MessageBox.Show("Kayıt İşlemi Gerçekleşti.");
}
Posted
Updated 19-Sep-23 11:03am
v6
Comments
[no name] 17-Sep-23 14:18pm    
Check the progress bar's "Maximum" with your "%" ... the default, I believe, is 100.

The only indexing you are using is to access the rows and cells - at a guess you don't have sufficient cells in your DataGridView, so use the dubugger to check exactly what is happening while your code is running.

But ... never use DoEvents: it's a poor way to bodge round a problem. And in this case, it's bodging round a problem you don't have, because the code using DoEvents is on a separate thread already and that thread doesn't have a message pump so DoEvents will do nothing useful (if it isn't actually causing your app to crash already).

Plus ... that code looks like it wasn't thought about too much:
int percents = (i * 100) / total;
Given that total is an integer and you set it to 100 but never change it, that code is equivelant to writing
int percents = i;
but with more wasted processor time ...
 
Share this answer
 
I have solved the problem with the following codes. Thanks everybody.

public void DoProcessing(IProgress<int> progress)
{
    for (int i = 0; i != 100; ++i)
    {
        Thread.Sleep(100); // CPU-bound work
        if (progress != null)
            progress.Report(i);
    }
}


progressBar1.Value= 0;
progressBar1.Visible = true;
label1.Visible = true;


var progress = new Progress<int>(percent =>
{
    progressBar1.Value = percent;
    progressBar1.PerformStep();
    label1.Text= String.Format("Yükleme yüzdesi: {0} %", percent);
});

// DoProcessing is run on the thread pool.
await Task.Run(() => DoProcessing(progress));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900