Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am building an winapp.

A button which starts the timer1.

A progressBar1 which syncs with timer tick.

A button which stops the timer.

A listBox1 which shows the current date and time to check the timer working.

Here is the code.
C#
private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 2000;
            this.listBox1.Items.Clear();
            progressBar1.Maximum = 10;
            timer1.Tick += new EventHandler(timer1_Tick);
        }


C#
private void timer1_Tick(object sender, EventArgs e)
        {

            if (progressBar1.Value != 10)
            {
                progressBar1.Value++;
                this.listBox1.Items.Add("Event raised at :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:nnn"));
            }

        }



The above code works well but the progressBar1 progress and timer stops after one loop only.

I want the timer to stop only when user wants and progressBar1 must stop only with timer.If progressBar1 is full it must start again.
Posted
Comments
[no name] 1-Jun-14 11:41am    
If the progressbar and timer stops after the timer ticks once then you need to debug your application and find out what is stopping the timer. The code that you have presented here works exactly like you would expect it to.
Member 10579673 1-Jun-14 11:54am    
After the progress completes once the timer and progressbar both stops.
[no name] 1-Jun-14 12:22pm    
Yes, that is correct. You have not written any code to make it reset so it appears to stop. The tick event would still be happening but once your progressbar is max value it just stays max value.
DamithSL 1-Jun-14 12:12pm    
you need only the progress bar handling in timer tick event?
Richard MacCutchan 1-Jun-14 12:13pm    
I just ran that code and it works correctly. I don't see any code to stop the timer.

please see the following example:

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.InitOthers();
        }

        // set initial properties
        private void InitOthers()
        {
            this.timer1.Tick += timer1_Tick;
            this.timer1.Interval = 1000;
            this.progressBar1.Maximum = 10;
            this.button1.Click += button1_Click;
        }

        // button click handler
        void button1_Click(object sender, EventArgs e)
        {
            // clear listbox items
            this.listBox1.Items.Clear();
            // reset timer
            this.timer1.Enabled = false;
            this.timer1.Enabled = true;
            // reset progress bar
            this.progressBar1.Value = 0;
        }

        // timer interval elapsed handler
        void timer1_Tick(object sender, EventArgs e)
        {
            if (this.progressBar1.Value < 10)
                this.progressBar1.Value++; // progress
            else
                this.timer1.Enabled = false; // stop timer

            this.listBox1.Items.Insert(0,
                string.Format("tick: {0}", DateTime.Now.ToString("HH:mm:ss.fff")));
        }
    }



There are some points with your code:
1- (in button1_Click) You add tick handler every time when you press button1 which means tick handler will run once for the first button1 click, twice for the second click, etc. I am not sure that is what you want.
2- (in timer1_Click) You handle the case of 'not equal to 10'. What about if 'equal'? It is good practice to think about 'else' part when you handle any 'if'. Of course, you don't have to and sometimes you don't need to, but I advice you think 'if' with its 'else' part.
3- (int button1_Click) 'timer1.Enable=true' doesn't reset timer1. If it is already enabled, it continues to tick according to start time. If you want to reset it, you have to 'stop' then 'start' again.
4- (in timer1_Tick) Your custom format string for datetime has an error. no format specifier exists like 'nnn'.

the most important point is actually mentioned in comments. use your debugger to see what happens.
 
Share this answer
 
Comments
Member 10579673 1-Jun-14 12:45pm    
Your Code Also Stops The timer and progressBar.What i wanted was continuous progress of progressbar till i stop the timer.
Vedat Ozan Oner 1-Jun-14 12:49pm    
Ok, then you can change 'else' part in the tick handler. you can rewind progress bar and it never stops until you manually stop it. if you like, I can update the solution.
Member 10579673 1-Jun-14 12:57pm    
@Vedat Ozan Oner It would be helpful if you do update your code
if you need to show the progress animation when you click start button and need to stop it when you click stop button you can simply do as below

start button click
C#
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;

stop button click
C#
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.MarqueeAnimationSpeed = 0;
 
Share this answer
 
Here Is What Worked For me

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TImer_Sync_WIth_ProGressbar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 2000;
            this.listBox1.Items.Clear();
            progressBar1.Maximum = 10;
            progressBar1.Maximum = 100;
            timer1.Tick += new EventHandler(timer1_Tick);
        }



        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value == 100)
            {
                progressBar1.Value = 10;
            }
            else
            {
                progressBar1.Value++;
                this.listBox1.Items.Add("Event raised at :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:nnn"));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900