Solved

Coding a Progress Bar

Posted on 2013-11-03
6
621 Views
Last Modified: 2013-12-07
Hello,
I have a c# application which reads through a transaction file processing each record within the transaction (could be 200 records per transaction). I have a Driver.cs program reading through each of the transactions from sequence 1 to last sequence. Some of the transactions will have an input requirement (indicated by a flag on the transaction itself) and when such a flag is encountered a call is made to an new form "InputForm1.cs" to take in the required input. These inputs are the only user interface in processing the transactions. I need to show a "Progress bar"  to indicate to users that application is processing (especially when it in processing non input transactions).

I have tried putting a progress bar into the InputForm1 form and then calling the InputForm1 for the non-input transactions  as well as input transactions and for the non input transactions, displaying the progress bar, but this just produces a lot of flicker.

Appreciate if anyone can advise on the best method of displaying a progress bar in these circumstances that will give a seamless display of % progress when non input transactions are being processed.

thanks
Pat
0
Comment
Question by:pclarke7
  • 3
  • 2
6 Comments
 
LVL 2

Assisted Solution

by:Pavel_Kudishin
Pavel_Kudishin earned 334 total points
ID: 39619980
You need to process your transaction in separate background thread (e.g. use BackgroundWorker class which can reporting progress to UI. See usage example by the previous link
0
 

Author Comment

by:pclarke7
ID: 39620024
Hi Pavel,
thanks for your comment. I can't see how this will solve the issue. If, for example, my 2nd and 100th Sequence within the transaction require an input. The the InputForm1 will be called initially on 2nd transaction and will be closed until until the 100th transaction - which could be a 30 second delay. In this instance how and where can I keep user informed of progress.

regards
Pat
0
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 166 total points
ID: 39620701
The easiest method would be to display the progress bar with continuous display. This way you do not need to worry about updating the progress bar.

here is a good article on progress bars

http://www.codeproject.com/Articles/449594/Progress-Bars-Threads-Windows-Forms-and-You

Michael
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Accepted Solution

by:
Pavel_Kudishin earned 334 total points
ID: 39620743
If you are developing ASP.NET application then use timer and ajax on you page to request progress from server and to update progress bar on the page.

Also with ajax request from the server transaction's queue which required input from user and display on the page input controls for user.

Transaction on the server need to be processed with custom windows service which builds transaction queue from transactions which require user input
0
 

Author Comment

by:pclarke7
ID: 39628812
I have tried using a background worker to run the progressBar however I am not getting beyond 1% in the following example.  In the Form1_Load I have an iteration inside which I want to display the ProgressBar. Appreciate if someone can point out where I am going wrong with this logic.

Pat



namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        BackgroundWorker bgw = new BackgroundWorker();
        float tot1=100;
        float cnt1=0;
        float rslt = 0;
        int percents = 0;
        public Form1()
        {
            InitializeComponent();
            label1.Text = "";
            label2.Text = "";
        }


        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
             {
                System.Threading.Thread.Sleep(500);
                rslt = ((cnt1 / tot1) * 100);
                percents = Convert.ToInt32(rslt);
                bgw.ReportProgress(percents, cnt1);
             }
        }

        void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
            label2.Text = String.Format("Total items transfered: {0}", e.UserState);
        }

        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //do the code when bgv completes its work
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (cnt1 = 0; cnt1 <= tot1; cnt1++) //some number (total)
            {
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                bgw.WorkerReportsProgress = true;
                if (!bgw.IsBusy)
                {
                    bgw.RunWorkerAsync();
                }
            }
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
      }
    }
0
 

Author Closing Comment

by:pclarke7
ID: 39703018
Thanks for your help. Working nicely now
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

838 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