Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am very new to C# i used the following code for my component class when i add this is to my windows application and calling start function the timer is not elapsing and the count value is not displaying what may the problem can anyone clear it.


C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.ComponentModel;
namespace timerer
{
    public class timercomp:Component
    {
        public int count=0;

        System.Timers.Timer myTimer = new System.Timers.Timer();

        public timercomp(IContainer container)
        {
            container.Add(this);
             }
             public int start()
             {

                myTimer.Enabled = true;
                 myTimer.Interval += 1000;
                 myTimer.Elapsed += new ElapsedEventHandler(OnTimer);
                myTimer.AutoReset = true;
                 return count;
             }
        public void OnTimer(Object source, ElapsedEventArgs e)
        {
            myTimer.Enabled = false;
            myTimer.Interval += 1000;
            count = count + 1;
            start();

        }

    }
}
Posted
Comments
[no name] 13-Sep-12 10:50am    
Where is it that you call MyTimer.Start()?
[no name] 13-Sep-12 10:52am    
And you do realize that everytime that your timer event fires, you are adding a new event handler right? And return count makes no sense?

If you want a timer, you should just use one of the existing timers (System.Windows.Forms.Timer or System.Threading.Timer). Your component doesn't make sense and is completely unnecessary.
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.ComponentModel;
namespace timerer
{
    public class timercomp:Component
    {
        public int count=0;
 
        System.Timers.Timer myTimer = new System.Timers.Timer();
 
        public timercomp(IContainer container)
        {
            container.Add(this);
            myTimer.Elapsed += new ElapsedEventHandler(OnTimer);
        }
        
        public int start()
        {
 
                myTimer.Enabled = true;
                 myTimer.Interval += 1000;
                 myTimer.AutoReset = true;
                 return count;
        }
        
        public void OnTimer(Object source, ElapsedEventArgs e)
        {
            myTimer.Enabled = false;
            myTimer.Interval += 1000;
            count = count + 1;
            start();
 
        }
 
    }
}

//somewhere in your code you need
timercomp comp = new timercomp();
timercomp.start();


don't know why you return count in start, when it is called outside the code above it will always return 0. You should see it incrementing using the debugger though.
Note your intervals are getting bigger and bigger, don't know if that was your intention.
 
Share this answer
 
v2

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