locked
Progress Bar looping iteration issue RRS feed

  • Question

  • I am using progress bar in my C# win form application.

    normally i am using many for loops and each for loop will have more than 1000 (thousand) iterations.

    for example, below i am using 3 for loops and will have 100o iterations.

    when i use the progress bar for every loop, the count will not come perfect.

    For first loop, i will set "40%", 

          Second loop, will have maximum "40%"

         Third Loop, will have maximun "20%".

    All the loops should be executed within 100%.

    But, In my code, First loop will be executing 100%. even though 100% completed still it is running first loop.

    How to split the progress bar percentage while using separate loop.

    below is the code

    progbar1.Minimum  = 0;
    progbar1.Maximum = 40;
    progbar1.Step = 1;
    //progbar1.Value = 0;
    int idxcount1 = 1;
    foreach (DataRow DR in dtTable1.Rows) //Total Rows = 1000 rows
    {
    
       progbar1.Value = idxcount1;
       idxcount1++; 
    } //Here it should take and show only "40%" of all the rows executed
    
    progbar1.Maximum = 80;
    progbar1.Step = 1;
    //progbar1.Value = 0;
    int idxcount2 = 1;
    foreach (DataRow DR in dtTable2.Rows) //Total Rows = 1000 rows
    {
    
       progbar1.Value = idxcount2;
       idxcount2++; 
    } //Here it should take and show only "40%" of all the rows executed
    
    progbar1.Maximum = 80;
    progbar1.Step = 1;
    //progbar1.Value = 0;
    int idxcount3 = 1;
    foreach (DataRow DR in dtTable3.Rows) //Total Rows = 500 rows
    {
    
       progbar1.Value = idxcount3;
       idxcount3++; 
    } //Here it should take and show only "20%" of all the rows executed

    Friday, November 6, 2020 5:29 AM

Answers

  • If you want 100% for each table, then try something like this:

    progbar1.Step = 1;

    progbar1.Value = 0;

    rogbar1.Minimum  = 0;

    progbar1.Maximum = dtTable1.Rows.Count;

    foreach (DataRow DR in dtTable1.Rows)

    {

       progbar1.Value++;

    }

    progbar1.Value = 0;

    progbar1.Maximum = dtTable2.Rows.Count;

    foreach (DataRow DR in dtTable2.Rows)

    {

       progbar1.Value++;

    }

    progbar1.Value = 0;

    progbar1.Maximum = dtTable3.Rows.Count;

    foreach (DataRow DR in dtTable3.Rows)

    {

       progbar1.Value++;

    }

    • Proposed as answer by CoolDadTx Friday, November 6, 2020 4:00 PM
    • Marked as answer by Gani tpt Tuesday, November 10, 2020 2:05 AM
    Friday, November 6, 2020 9:48 AM
  • Place a label above or bellow the progress bar and use code like this:

       label1.Text = “First process”; label1.Update();

    If something else is required, maybe it has sense to open a new special question.


    • Edited by Viorel_MVP Monday, November 9, 2020 3:05 PM
    • Marked as answer by Gani tpt Tuesday, November 10, 2020 2:05 AM
    Monday, November 9, 2020 3:04 PM

All replies

  • Try something like this:

    progbar1.Minimum  = 0;

    progbar1.Maximum = dtTable1.Rows.Count + dtTable2.Rows.Count + dtTable3.Rows.Count;

    progbar1.Step = 1;

    foreach (DataRow DR in dtTable1.Rows)

    {

       progbar1.Increment()

    }

    foreach (DataRow DR in dtTable2.Rows)

    {

       progbar1.Increment()

    }

    foreach (DataRow DR in dtTable3.Rows)

    {

       progbar1.Increment()

    }



    • Edited by Viorel_MVP Friday, November 6, 2020 6:07 AM
    Friday, November 6, 2020 6:06 AM
  • Error in progbar1.Increment() ==> No Overload for method 'Increment' takes 0 argument..

    Anything need to pass value in the increment...?

    Friday, November 6, 2020 6:34 AM
  • i want to handle individual progress for loop.

    for example, first for loop it should complete 100% or some partial percent.

    second for loop should complete 100% or some partial percentage. and third like that.

    how to do this...?

    Friday, November 6, 2020 8:40 AM
  • Hi Gani,

    Thank you for posting here.

    I think Viorel should mean progbar1.Increment(1);

    He may be writing code by hand, but his idea is no problem, and the progress bar can work normally.

    The only problem is that although the number of data in the three datatables is 2:2:1, the different datatable structure will cause their loading time to be not strictly 40:40:20. Is this OK now? Or do you want it to strictly follow this ratio, as if the progress bar is divided into three separate parts?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 6, 2020 8:50 AM
  • If you want 100% for each table, then try something like this:

    progbar1.Step = 1;

    progbar1.Value = 0;

    rogbar1.Minimum  = 0;

    progbar1.Maximum = dtTable1.Rows.Count;

    foreach (DataRow DR in dtTable1.Rows)

    {

       progbar1.Value++;

    }

    progbar1.Value = 0;

    progbar1.Maximum = dtTable2.Rows.Count;

    foreach (DataRow DR in dtTable2.Rows)

    {

       progbar1.Value++;

    }

    progbar1.Value = 0;

    progbar1.Maximum = dtTable3.Rows.Count;

    foreach (DataRow DR in dtTable3.Rows)

    {

       progbar1.Value++;

    }

    • Proposed as answer by CoolDadTx Friday, November 6, 2020 4:00 PM
    • Marked as answer by Gani tpt Tuesday, November 10, 2020 2:05 AM
    Friday, November 6, 2020 9:48 AM
  • Thanks Timon...

    yes. i want progress bar is divided into three separate parts 40:40:20 ratio..??


    • Edited by Gani tpt Monday, November 9, 2020 6:07 AM
    Friday, November 6, 2020 10:52 AM
  • I have another technical doubt.

    At every for loop, could we give text like "First process..Started", then at the ending first loop "First process..ended"..?

    for second loop, "Second process....Started", then at the ending second loop "Secon process..ended"

    progbar1.Step = 1;

    progbar1.Value = 0;

    progbar1.Minimum  = 0;

    progbar1.Text = "First process...Started"; ==> Will this work..?

    progbar1.Maximum = dtTable1.Rows.Count;

    foreach (DataRow DR in dtTable1.Rows)

    {

       progbar1.Value++;

    }

    progbar1.Text = "First process...Ended"; ==> Will this work..?

    progbar1.Value = 0;

    progbar1.Text = "Second process...Started"; ==> Will this work..?

    progbar1.Maximum = dtTable2.Rows.Count;

    foreach (DataRow DR in dtTable2.Rows)

    {

       progbar1.Value++;

    }

    progbar1.Text = "Second process...Ended";  ==> Will this work..?


    • Edited by Gani tpt Monday, November 9, 2020 6:07 AM
    Monday, November 9, 2020 6:07 AM
  • any update...
    Monday, November 9, 2020 10:27 AM
  • any update...

    pls. find my last reply...

    Monday, November 9, 2020 10:28 AM
  • Place a label above or bellow the progress bar and use code like this:

       label1.Text = “First process”; label1.Update();

    If something else is required, maybe it has sense to open a new special question.


    • Edited by Viorel_MVP Monday, November 9, 2020 3:05 PM
    • Marked as answer by Gani tpt Tuesday, November 10, 2020 2:05 AM
    Monday, November 9, 2020 3:04 PM