Solved

Progress Bar in Winows Application C#

Posted on 2006-11-13
34
516 Views
Last Modified: 2008-03-10
Hi,

I am working on C# and making windows Application.

My Applications retrieves 1000's of records and process it.

So I want to add progress bar to my application.

My progress bar should tell the Time left and percentage compeleted.

It would be good if it tells that no of records completed out of the no of records.

I appricate your easy example and quick help.

Thanks in Advance,

Kind Regards,
0
Comment
Question by:dinesh_bali
  • 17
  • 11
  • 6
34 Comments
 
LVL 6

Expert Comment

by:so3
ID: 17933990
       //put a ProgressBar on the form

            progressBar1.Step = 1;
            progressBar1.Maximum = 1000;// the maximum records
            Label prg_percent = new Label();  // procent label
            Label prg_rec_com = new Label(); // no record and total records Label
            for (int i = 0; i < progressBar1.Maximum - 1; i++)
            {
                //fetch data code
                progressBar1.Value = i; //updates the progress bar
                prg_percent.Text = Math.Round((i / progressBar1.Maximum) * 100, 2);
                prg_rec_com.Text = "Record " + i + "/" + progressBar1.Maximum;
                // for time you have to calculate or by using a timer or by calculating from time start and current time
0
 
LVL 6

Expert Comment

by:so3
ID: 17934032
Sorry
the line prg_percent.Text = Math.Round((i / progressBar1.Maximum) * 100, 2);
should be
prg_percent.Text = System.Convert.ToString(Math.Round((double)((i/progressBar1.Maximum) * 100),2));
0
 

Author Comment

by:dinesh_bali
ID: 17934182
Ok, I will try this solution.

I heard that progress bar do not reflect progress unless event is sent?

Are you sure that this will work.

Can you also help me for the timer?

Thanks in advance
Kind Regards
0
 

Author Comment

by:dinesh_bali
ID: 17934743
Do you have the running example or sample code. I will try the above given code.?
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17936913
The progressbar should work with code above. If it doesn't reflect any progress, add this code to the end of the 'for' section:
progressBar1.Invalidate();
this.Update();

Now for the timer, add a timer and a label (clear; no text) to your form.
Add an int variable to the main section of your class ('global' variable):

int seconds = 0;

My timer is called timer1 and my label is called labelTime.
Set the Interval property of the timer to 1000 and add a Tick event:

        private void timer1_Tick(object sender, EventArgs e)
        {
            seconds++;

            TimeSpan ts = new TimeSpan(0, 0, seconds);
            TimeSpanConverter tsc = new TimeSpanConverter();
            labelTimer.Text = tsc.ConvertToString(ts);
        }

Add this code just before the 'for' loop of the progressbar:
seconds = 0;
timer1.Enabled = true;

Add this code just after the 'for' loop:
timer1.Enabled = false;

You now should have a running timer.

I hope this helps. If you have any questions on my code, feel free to ask.
0
 
LVL 6

Expert Comment

by:so3
ID: 17937712
for the timer fallow RubenvdLinden  indications
0
 

Author Comment

by:dinesh_bali
ID: 17942781
Hi,

I have tried the code

My Progress bar is not working, but the calculation of records completed are showing on the screen from your below code as mentioned above:

prg_percent.Text = Math.Round((i / progressBar1.Maximum) * 100, 2);
prg_rec_com.Text = "Record " + i + "/" + progressBar1.Maximum;

But with this my progress bar is not updating.

For Timer Problem:

On button click If I want this timer to start, then where should I place the code.
If I write this code on onClick of button then the it stops in 00:00:01

Where should I place this code.

I am updating my code, I have deleted the code and make it simple so that u can understand in one go and help me.

I am updating in next form submit
0
 

Author Comment

by:dinesh_bali
ID: 17942796
// This class is called when button is clicked from the form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Mynamespace
{
    public partial class Form1 : Form
    {
        private ProgressBar progressBar1 = new ProgressBar();
        static int seconds = 0;
        TimeSpan ts = new TimeSpan(0, 0, seconds);
        TimeSpanConverter tsc = new TimeSpanConverter();      
                   
        public Form1()
        {
            InitializeComponent();
        }
       
        /*
        This method is called on click event of the form and this method calls
        method getCustomerDetails(strDcopCulture, this);
        where database interaction is done and the value of records increases by time
        */
       
      private void button1_Click(object sender, EventArgs e)
      {
          string strDcopCulture = cbxSelectCountry.SelectedValue.ToString();

          string message = "HERE MY TEXT GOES: " + this.getCountrySiteDesc(strDcopCulture);
          string caption = "Confirm Box";

          MessageBoxButtons buttons = MessageBoxButtons.YesNo;
          DialogResult result;

          // Displays the MessageBox.

          result = MessageBox.Show(this, message, caption, buttons);

          if (result == DialogResult.Yes)
          {

            seconds++;

            ts = new TimeSpan(0, 0, seconds);
            tsc = new TimeSpanConverter();
            this.lblTimer.Text = tsc.ConvertToString(ts);

            CustomerMasterValue custValue = null;
            CustomerProcess custProc = new CustomerProcess();

            custValue = custProc.getCustomerDetails(strDcopCulture, this);
            lblStatus.Text = "Status: ABC for " + strDcopCulture + " is Completed.";                
          }
      }
      
      /*
      
            This method should update progressbar, but it is not. It is only updating the No of records updated out of max no of records.
      */

        public void updateProgressBar(int maxRecords, int recCount)
        {
            progressBar1.Step = 1;
            progressBar1.Maximum = maxRecords;// the maximum records
            this.cntRecordsExe.Text = Convert.ToString(maxRecords);
            Label prg_percent = new Label();  // procent label
            Label prg_rec_com = new Label(); // no record and total records Label
            this.cntRecordsExe = prg_percent;

            //fetch data code
            progressBar1.Value = recCount; //updates the progress bar
            this.lbl_prg_rec_com.Text = System.Convert.ToString(Math.Round((double)((recCount / progressBar1.Maximum) * 100), 2));
            this.lbl_prg_percent.Text = "Record " + recCount + "/" + progressBar1.Maximum;
           
            progressBar1.Invalidate();

            seconds++;

            ts = new TimeSpan(0, 0, seconds);
            tsc = new TimeSpanConverter();
            this.lblTimer.Text = tsc.ConvertToString(ts);

            this.Update();
         }
    }
}


// This class has the database interaction and calls the updateProgressBar() to update progressbar to update on the form.
Also help me with the timer

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Windows.Forms;

namespace Mynamespace.Process
{
    class CustomerProcess    {
       
        public int statusFlag = 0;
        public int Count = 0;

/*
      getCustomerDetails() method is called on the button click event from the form
      Which has the database interaction.
      To update Progressbar updateProgressBar() method is called

*/
        public CustomerMasterValue getCustomerDetails(String strDcopCulture, Form1 frm)
        {
            SqlConnection conCustomerDetails = new DbConnection().Connection();                      

            CustomerMasterValue custData = new CustomerMasterValue();
            String queryText = "Query goes here";

            SqlCommand objCmd = new SqlCommand(queryText, conCustomerDetails);
            objCmd.CommandTimeout = 250;
            SqlDataReader objRdr = null;

               
                objRdr = objCmd.ExecuteReader();
               
                while (objRdr.Read())
                {
                   
                    Count++;
                   
                    frm.updateProgressBar(31015, Count); // Progress Bar Method called
                }
           

            return custData;
        } // End of class getCustomerDetails

       
    } // End of class CustomerProcessBean
} // End of namespace

I Hope to have your full co-operation

Thanks in Advance
0
 
LVL 6

Expert Comment

by:so3
ID: 17945130
try to set the position of the progressbar
            progressBar1.Position= recCount; //moves the position of the progressbar
            progressBar1.Update(); //to update the progressbar

public void updateProgressBar(int maxRecords, int recCount)
        {
            progressBar1.Step = 1;
            progressBar1.Maximum = maxRecords;// the maximum records
            this.cntRecordsExe.Text = Convert.ToString(maxRecords);
            Label prg_percent = new Label();  // procent label
            Label prg_rec_com = new Label(); // no record and total records Label
            this.cntRecordsExe = prg_percent;

            //fetch data code
            progressBar1.Position= recCount; //updates the progress bar
            progressBar1.Update(); //to update th eprogressbar
            this.lbl_prg_rec_com.Text = System.Convert.ToString(Math.Round((double)((recCount / progressBar1.Maximum) * 100), 2));
            this.lbl_prg_percent.Text = "Record " + recCount + "/" + progressBar1.Maximum;
           
            progressBar1.Invalidate();

            seconds++;

            ts = new TimeSpan(0, 0, seconds);
            tsc = new TimeSpanConverter();
            this.lblTimer.Text = tsc.ConvertToString(ts);

            this.Update();
         }
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17945311
You start your class with

public partial class Form1 : Form
    {
        private ProgressBar progressBar1 = new ProgressBar();
        static int seconds = 0;
        TimeSpan ts = new TimeSpan(0, 0, seconds);
        TimeSpanConverter tsc = new TimeSpanConverter();
        (etc)

1. Why do you manually add the ProgressBar? If this is all your code, I can understand why it won't update; it's not added to the control collection of the form.
Remove this line and add the ProgressBar using the Windows Forms Designer.
2. int seconds should only be static if you have several instances of your form sharing the same variable; if this is not the case, remove 'static'.
3. TimeSpan and TimeSpanConverter do not need to be declared here, just remove these lines.

Now let's take a look to the updateProgressBar method. Updating the ProgressBar doesn't automatically mean you need to update the time displayed, so remove these lines from the updateProgressBar method:

            seconds++;

            ts = new TimeSpan(0, 0, seconds);
            tsc = new TimeSpanConverter();
            this.lblTimer.Text = tsc.ConvertToString(ts);

OK, now we can look at your timer. How does CustomerProcess work? Is it some kind of thread or backgroundworkerprocess? If so, you'll have to check that the process has ended before disabling the timer in the OnClick event:

private void button1_Click(object sender, EventArgs e)
     {
         string strDcopCulture = cbxSelectCountry.SelectedValue.ToString();

         string message = "HERE MY TEXT GOES: " + this.getCountrySiteDesc(strDcopCulture);
         string caption = "Confirm Box";

         MessageBoxButtons buttons = MessageBoxButtons.YesNo;
         DialogResult result;

         // Displays the MessageBox.

         result = MessageBox.Show(this, message, caption, buttons);

         if (result == DialogResult.Yes)
         {
          timer1.Enabled = true;

          CustomerMasterValue custValue = null;
          CustomerProcess custProc = new CustomerProcess();

          // Implement some loop code to check if CustomerProcess is finished
          custValue = custProc.getCustomerDetails(strDcopCulture, this);
          lblStatus.Text = "Status: ABC for " + strDcopCulture + " is Completed.";
          timer1.Enabled = false;
         }
     }

Just add the timer Tick event like I posted yesterday and it should work.
0
 

Author Comment

by:dinesh_bali
ID: 17945356
HI,

I tried to

add the following lines:

    progressBar1.Position = recCount; //moves the position of the progressbar
    progressBar1.Update(); //to update the progressbar

 in the method updateProgressBar(int maxRecords, int recCount)

but it gives error that
'System.Windows.Forms.ProgressBar' does not contain a definition for 'Position'

Here is my updated code for this method

public void updateProgressBar(int maxRecords, int recCount)
{
    progressBar1.Step = 1;
    progressBar1.Maximum = maxRecords;// the maximum records
    this.cntRecordsExe.Text = Convert.ToString(maxRecords);
    Label prg_percent = new Label();  // procent label
    Label prg_rec_com = new Label(); // no record and total records Label
    this.cntRecordsExe = prg_percent;

    //fetch data code
    progressBar1.Value = recCount; //updates the progress bar
    this.lbl_prg_rec_com.Text = System.Convert.ToString(Math.Round((double)((recCount / progressBar1.Maximum) * 100), 2));
    this.lbl_prg_percent.Text = "Record " + recCount + "/" + progressBar1.Maximum;
    progressBar1.Invalidate();
   
    progressBar1.Position = recCount; //moves the position of the progressbar
    progressBar1.Update(); //to update the progressbar

    this.Update();
}


MY PROGRESS BAR METHOD
updateProgressBar()

IS CALLED FROM THE METHOD

getCustomerDetails(String strDcopCulture, Form1 frm)

CODE FOR THIS METHOD IS WRITTEN ABOVE.


Thanks in Advance for trying to help me
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17945409
The Position property is invalid for a ProgressBar; you can remove this line.
Did you try it after applying my tip to add the ProgressBar using the Windows Forms Designer?
0
 

Author Comment

by:dinesh_bali
ID: 17945457
Yes, I tried it without this line.

and also I have dragged Progressbar in the form using Windows form designer

But still progressbar is not working.

Please ask if anymore information you require from me.

Kind Regards,
Dinesh
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17945498
If you place a breakpoint on this line

progressBar1.Value = recCount; //updates the progress bar

and debug, do you see it update?


Another test, change the line to

progressBar1.Value = 31015;

Considering the value you give for maxRecords (also 31015),  it should be filled now.
Please tell me what you see.
0
 

Author Comment

by:dinesh_bali
ID: 17945514
Yes, Now I have removed the line

ProgressBar progressBar1 = new ProgressBar();
from the code
and

changed my code to
this.progressBar1

where ever it was progressBar1

and I got some effect in progressbar.

I will check more and come back to you.

Thanks

0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17945542
'progressBar1' equals 'this.progressBar1', so I really think it's the line you removed.
However, good to hear you made some progress ;-)

Please let me know if you can get the ProgressBar and the Timer working now. If you have any further problems on this, please feel free to ask.
0
 

Author Comment

by:dinesh_bali
ID: 17945683
Many thanks for helping me.

Now my Progressbar shows the progress. But it stops screen refreshing after some time, but at the back If I see then I see that task is going perfect only refereshing to the screen is stopped.

And also the timer is still not working.

I am submitting my updated code in next submission.

Can we calculate the estimate time left in completing the process. Basically this is my main moto to do with the timer.

Yes, you asked me the question that
How does CustomerProcess work?
Basically this is a process file which has the database interaction, database has the thousands of records, inside the while loop of CustomerProcess I called the method updateProgressBar()


Now If you see CustomerProcess more deeply u will understand this.

I am submiting by updated code in the next submission and deleted unwanted code for you.

Thanks again for all u are doing.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:dinesh_bali
ID: 17945684
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace My Namesapace
{
    public partial class Form1 : Form
    {
        int seconds = 0;        
       
     
             
        public Form1()
        {
            InitializeComponent();
        }

       

       

        private void button1_Click(object sender, EventArgs e)
        {
            string strDcopCulture = cbxSelectCountry.SelectedValue.ToString();

            string message = "Do you really want to migrate the data for Country Site: " + this.getCountrySiteDesc(strDcopCulture);
            string caption = "Confirm Box";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            // Displays the MessageBox.

            result = MessageBox.Show(this, message, caption, buttons);

            if (result == DialogResult.Yes)
            {
                timer1.Enabled = true;

                lblStatus.Text = "Status: Migrating Data for " + strDcopCulture + " Country.   Please Wait....";
                btnMigrateData.Enabled = false;
                btnFailedReport.Enabled = false;

                CustomerMasterValue custValue = null;
                CustomerProcess custProc = new CustomerProcess();

                custValue = custProc.getCustomerDetails(strDcopCulture, this);
                lblStatus.Text = "Status: Data Migrating for " + strDcopCulture + " Country is Completed.";
                btnMigrateData.Enabled = true;
                btnFailedReport.Enabled = true;

                timer1.Enabled = false;
            }
        }

     
        public void updateProgressBar(int maxRecords, int recCount)
        {
            this.progressBar1.Step = 1;
            this.progressBar1.Maximum = maxRecords;// the maximum records
            this.cntRecordsExe.Text = Convert.ToString(maxRecords);            
            //this.cntRecordsExe = prg_percent;

            //fetch data code
            this.progressBar1.Value = recCount; //updates the progress bar
            this.lbl_prg_rec_com.Text = System.Convert.ToString(Math.Round((double)((recCount / progressBar1.Maximum) * 100), 2));
            this.lbl_prg_percent.Text = "Record " + recCount + "/" + this.progressBar1.Maximum;
            this.progressBar1.Invalidate();
            //progressBar1.Position = recCount; //moves the position of the progressbar
            this.progressBar1.Update(); //to update the progressbar            
            this.Update();            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            seconds++;

            TimeSpan ts = new TimeSpan(0, 0, seconds);
            TimeSpanConverter tsc = new TimeSpanConverter();
            lblTimer.Text = tsc.ConvertToString(ts);            
        }


        private void progressBar2_Click(object sender, EventArgs e)
        {

        }
    }
}
0
 
LVL 6

Expert Comment

by:so3
ID: 17945771
sorry i've wrote position instead of value in the above comment.

<<But it stops screen refreshing after some time
do you have many records in database maybe you need to use a backgroundworker(thread) to fetch data
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17945885
OK, the timer still doesn't work. Doesn't it work at all or is there some progress?
Can you breakpoint the Tick event to see if it fires?

To answer the next question in advance, to calculate an estimate you could let your process run until it reaches 10 percent, then get the time it took to reach this point (just get the value of the int variable seconds) and then calculate how much seconds it would take to do this 9 more times, e.g.

1. Create a form-wide variable:
int secondsRemaining = 0;

2. Add a label (lblTimeRemaining)

3. Modify updateProgressBar:
public void updateProgressBar(int maxRecords, int recCount)
        {
            this.progressBar1.Step = 1;
            this.progressBar1.Maximum = maxRecords;// the maximum records
            this.cntRecordsExe.Text = Convert.ToString(maxRecords);            
            //this.cntRecordsExe = prg_percent;

            //fetch data code
            this.progressBar1.Value = recCount; //updates the progress bar
            this.lbl_prg_rec_com.Text = System.Convert.ToString(Math.Round((double)((recCount / progressBar1.Maximum) * 100), 2));
            this.lbl_prg_percent.Text = "Record " + recCount + "/" + this.progressBar1.Maximum;
            this.progressBar1.Invalidate();
            //progressBar1.Position = recCount; //moves the position of the progressbar
            this.progressBar1.Update(); //to update the progressbar            
            this.Update();

            if (secondsRemaining != 0 && ((recCount / maxRecords) * 100) >= 10)
            {
                 secondsRemaining = seconds * 9;
                 TimeSpan ts = new TimeSpan(0, 0, secondsRemaining)
                 TimeSpanConverter tsc = new TimeSpanConverter()
                 lblTimeRemaining.Text = tsc.Convert.ToString(ts);
            }
        }

4. Modify the timer event code
private void timer1_Tick(object sender, EventArgs e)
        {
            seconds++;

            TimeSpan ts = new TimeSpan(0, 0, seconds);
            TimeSpanConverter tsc = new TimeSpanConverter();
            lblTimer.Text = tsc.ConvertToString(ts);

            if (secondsRemaining > 0)
            {
                 TimeSpan tsr = new TimeSpan(0, 0, secondsRemaining);
                 lblTimeRemaining = tsc.Convert.ToString(tsr);
            }          
        }

Unfortunately I haven't been able to test this code yet.
However, first we have to get your timer working before you can implement this code. If it gives any problems, feel free to ask.
0
 

Author Comment

by:dinesh_bali
ID: 17945983
1. I have breakpoint in method
private void timer1_Tick(object sender, EventArgs e)

It is not going in this method?
Now what should I do that it should come in this method.

2. My screen stops refreshing with progressbar and records completed after some time, but at the back the process is running perfect.

What should I write to forcibly refresh the screen and should not stop.

Also, If I minimise the screen and then I maximise then also screen stops refreshing the progressbar and records count.

Now, what should I go about this.

Thanks for all you are doing.
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17946019
Timer problem:
I think you haven't created an event, but just copied the code of the event your own code.
It's probably easy to solve. Open your form in design mode and right-click the timer. From the context menu, click 'Properties'.
You'll now see the properties like Enabled and Interval. Make sure Enabled is set to 'False' and Interval is set to '1000'.
Now, in the Property Dialog, click the little icon with the picture of lightning. You'll now see the events. A Timer control has only one event, Tick.
Click the dropdown next to Tick and select 'timer1_Tick'.

Rebuild and run your project. The event code should fire now.

ProgressBar problem:
I agree with so3, you should probably use a backgroundworker or a separate thread to solve this.
0
 

Author Comment

by:dinesh_bali
ID: 17946137
1) I have right clicked on Timer

then goes to properties

I have now the following in properties

1. PropertyBinding ---> There is small glowing icon. I clicked and found 3 options

      A. Enabled
      B. Interval
      C. Tag

      All have none values

      Now where I found the option to select event 'timer1_Tick'
      

2. Name: timer1
3. Enabled: False
4. GenerateMember: True
5. Interval: 1000
6. Modifiers: Private
7. Tag:

Now what should I do?

2)

How to use backgroundworker or how can I make the seperate thread to solve this problem.

Can I post another question for this in the expert-exchange

 Thanks for all you are doing to me.
 
 Kind Regards,
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17946236
Timer problem:
Leave all the values of 'PropertyBinding' empty, you don't need them to solve this problem.
All the other properties are OK.

To get to events section, you need to find a button with orange lightning on the toolbar of the Properties Window.
The toolbar is directly under the combobox with the text 'timer1    System.Windows.Forms.Timer'.
If you move your mouse over the button with orange lightning , you will see a yellow tooltip with the text 'Events'.

If you really can't find it, there's one alternative. Add this line to the constructor of your form:
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);



ProgressBar problem:
There are several nice articles (including source code) on the BackgroundWorker compont.
This one even uses the ProgressBar:
http://dotnetaddict.dotnetdevelopersjournal.com/net_20_backgroundworker_component_demo.htm


I hope this helps
0
 

Author Comment

by:dinesh_bali
ID: 17946528
1) I found the orange arrow and I selected timer1_Tick

When I select Enabled = true from properties window, then when my form is loaded timer starts running.

But now I set Enabled = false, as I do not want this to start when my form loads.

This means my timer is start working.

I am not able to see the timer starts when I click the button,

onClick of the button method private void button1_Click(object sender, EventArgs e) is called. You can see this above

Also, I have done breakpoint in the method timer1_Tick(). It is not going in the method timer1_Tick()

What could be the possible reason.

I also done this.timer1.Enabled = true; and this.timer1.Enabled = false;

inside the button1_Click method.

2) I will check the url u sent for background worker, and If required I will post another question

Thanks
0
 
LVL 12

Accepted Solution

by:
RubenvdLinden earned 500 total points
ID: 17946599
OK, so the timer is disabled by default and has an Interval of 1000. The Tick event is assigned to the correct event.
When you click the button, you have a statement

timer1.Enabled = true;

This should all be correct. Can you remark all lines of code in the OnClick event EXCEPT for:
timer1.Enabled = true;
?
This will make sure that the Timer is not interrupted by your data process, like the ProgressBar is also interrupted by this process.

Now, if you run this code and click the button, the event of the timer should be fired.
Please let me know if this works, so we know where to look next.
0
 

Author Comment

by:dinesh_bali
ID: 17947728
Here is my code

private void button1_Click(object sender, EventArgs e)
        {
            string strDcopCulture = cbxSelectCountry.SelectedValue.ToString(); // Get Data from COMBOBOX from Screen

            string message = "Do you really want to migrate the data for Country Site: " + this.getCountrySiteDesc(strDcopCulture);
            string caption = "Confirm Box";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo; // This is for confirm box if user wants to do the process or not
            DialogResult result;

            // Displays the MessageBox.

            result = MessageBox.Show(this, message, caption, buttons);

            if (result == DialogResult.Yes) // If user selects Yes in the dialouge box
            {
                this.timer1.Enabled = true; // Here I wish my timer to start

                lblStatus.Text = "Status:  " + strDcopCulture + " Country.   Please Wait...."; // This is message on Label that work is in progress.
                btnMigrateData.Enabled = false; // Disabled Buttons on the main screen
                btnFailedReport.Enabled = false; // Disabled Buttons on the main screen

                CustomerMasterValue custValue = null; // This is Object returns by CustomerProcess
                CustomerProcess custProc = new CustomerProcess(); // Initialise the Process file who is responsible to interact with database

                custValue = custProc.getCustomerDetails(strDcopCulture, this); // Method in the class CustomerProcess, where database interaction is done and updateProgressBar() method is called from while loop when data is fetched from database, you can see the code of this above
               
                lblStatus.Text = "Status: " + strDcopCulture + "  Completed."; // Message on the label that task is completed
               
                btnMigrateData.Enabled = true; // Buttons are Enabled
               
                btnFailedReport.Enabled = true; // Buttons are Enabled

                this.timer1.Enabled = false; // Wants my timer to stop here
            }
        }
0
 

Author Comment

by:dinesh_bali
ID: 17950259
Hi RubenvdLinden,

I have posted the code as asked you with my comments. I am waiting for your reply.

Do u find some problem in my code.

Kind Regards,
Dinesh
0
 
LVL 6

Expert Comment

by:so3
ID: 17953954
on the progressbar with backgroundworker the demo providedd by RubenvdLinden  is great and will help you figure on how to change the code using the backgroundworker
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17954134
The code you posted looks OK to me.
However, I need to know if the event code (timer1_Tick) is fired. If it's not fired, can you remark (//) all the code in your 'if (result == DialogResult.Yes)' block EXCEPT the line
this.timer1.Enabled = true;

Please put a breakpoint on timer1_Tick to see if it's fired (because of the interval, after you click the button it will take 1 second before it fires).
0
 

Author Comment

by:dinesh_bali
ID: 17955105
Yes, I have commented all line except
this.timer1.Enabled = true;
and marked break point here.

and found that it is not going to the method timer1_Tick()

What could be the reason for this?

Regards,
Dinesh
0
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 17955182
I can think of two possibilities:
- The event is not set in the designer
- The timer is overwritten

1. Can you doublecheck that the event is set in the designer?
2. Can you check that you do not overwrite the timer somewhere (e.g. you haven't got

timer1 = new System.Timer();
or
timer1 = new System.Windows.Forms.Timer()

somewhere in your own code, like you also had with the ProgressBar)


As an alternative, you can add this line to your constructor code:
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

Keep the other lines in the OnClick event commented until it works.
Please let me know if this helps.
0
 

Author Comment

by:dinesh_bali
ID: 17969393
Hi  RubenvdLinden,

I am sorry for little late response.

I have seen my Timer Working, which has given me some conclusions, but I am not able to find that how Should I overcome this:

As you know, On my button click I have confirmation box, when user selects Yes, then I am trying to enable Timer.

Then my process starts with database, In while Loop from database, I am updating progress bar which updates progress bar and update whole screen with this.Update(); I think this resets my timer back.

As, I have prompted another message box if no datafound from the database. Now when this alert message comes, Timer starts working.

Now how can I have both Timer and Progress bar updated at the same time.

Hope to have your early response with some good help for me.

Regards,
0
 

Author Comment

by:dinesh_bali
ID: 17971383
My issue raised for this is solved. The Timer may not is running because of the form update. I will add Progressbar in panel and update it. So this may solve this problem.

But If I have some issue I will put new question.

Many Thanks for helping me and guide me that how progressbar and timer works

Thanks allot
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

18 Experts available now in Live!

Get 1:1 Help Now