Solved

Coding a Progress Bar

Posted on 2013-11-03
6
622 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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