Solved

dynamically change progress bar

Posted on 2006-11-03
5
271 Views
Last Modified: 2010-04-16
Hi the below code should ensure that my progress bar on the relevent import form accurately shows the progress of my import and that is my problem - getting the progress bar to accurately show the progress of the import. Please help!

public override bool Import ( string filename, params object[] additionalParameters )
            {
                  
                  for ( int i = 0; i < 100; i ++ ) //This is the problem line because there could be any number of records
                  {

                        // Ensure it hasn't been cancelled
                        if ( !this._cancelled )
                        {
                              this.Raise_ProgressUpdated ( "Importing", "Client " + i.ToString (), i );//This will show "Client" + record number
                                                                        //Do import here
                              System.Threading.Thread.Sleep ( 50 );
                        }
                        else
                        {
                              this.Raise_Complete ( false );
                              return false;
                        }

                  }

                  this.Raise_Complete ( true );

                  return true;

            }
0
Comment
Question by:JakeyCakes
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:redpipe
ID: 17865401
Loop through the file first to get the correct number of rows, and then set the maximum of your progressbar:

public int NumberOfLines(string path)
{
      int num = 0;
      using (StreamReader sr = new StreamReader(path, Encoding.Default)) {
            while (sr.ReadLine() != null) {
                  num++;
            }
      }
      return num;
}
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 17865418
You first have to check the number of records

    int n = number_of_records;
    // here make a callback or raise an event to set the correct max value in the progress bar

then

for (int i =0; i < n; i++)
{
    etc;
}

0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17865421
sorry, didn't see redpipe's post
0
 
LVL 8

Assisted Solution

by:redpipe
redpipe earned 250 total points
ID: 17865453
Another approach is to check if the maximum of the progressbar is reached and adjust it accordingly

using (StreamReader sr = new StreamReader(path, Encoding.Default)) {
  if ( !_cancelled )
    if(YourProgressBar.Value >= YourProgressBar.Maximum){
      //Choose an appropriate value for dynamic increase. Could also be 10% or whatever that suites your purpose
      YourProgressBar.Maximum = YourProgressBar.Maximum + 100;
      // => Continue doing your magic  :-)
    }
  } else {
  }
  // When your done importing or the user has cancelled. Set the progressbar to 100% or hide it, to indicate that your done
  YourProgressBar.Value = YourProgressBar.Maximum;
}
0
 

Author Comment

by:JakeyCakes
ID: 17866286
Thank you both for your help, I am going to split the points evenly.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rest webservice call over https via c# 6 41
Simple Injector with Web Service 4 41
MediaHelp 4 23
List<PaisEntity> - show some 1 24
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question