Solved

Coding a Progress Bar

Posted on 2013-11-03
6
611 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now