Click here to Skip to main content
14,927,528 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am develop window application project and i and use one class file in my project.In that class file i am write some method and property and i use that things in other form.Now i want to use Timer control in my class file so i am not take Timer control from Tool Box so how i initialize timer and it's tick event in my class file.
I have only .cs file not form's cs file.I initialize and generate tick event but not call tick event by timer that's problem.Here is my code:
I am start timer in serial port data receive event but when i start timer in other method then it will work proper.
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
tmr_rec = new Timer();
tmr_rec.Start();
tmr_rec.Interval = 1;
tmr_rec.Tick += new EventHandler(tmr_rec_Tick);
}
void tmr_rec_Tick(object sender, EventArgs e)
{
tmr_rec.Stop();
}
Posted
Updated 17-Feb-12 20:29pm
v2
Comments
BillWoodruff 17-Feb-12 0:04am
   
Are you asking how to use a 'Timer control in a Class which is not on a WinForm, WPF Window ?

Please tag your question to indicate the technology you are using.

Try this defined as a separate .cs file: Note we are using a separate NameSpace:
C#
namespace ClassWithTimer
{
    using System;
    using System.Timers;

    public class TimerInClass
    {
        public Timer TheTimer = new Timer();

        private int timerCount;

        // constructor
        public TimerInClass(double interval, bool startNow)
        {
            timerCount = 0;

            // this is not really necessary, since the default value
            // of a new System.Timer's 'AutoReset Property is 'true: 
            // shown here only for educational value
            //
            // if AutoReset is set to 'false: the Elapsed EventHandler 
            // is called once: the System.Timer must be re-started to use again
            TheTimer.AutoReset = true;

            TheTimer.Interval = interval;

            TheTimer.Elapsed += new ElapsedEventHandler(TheTimer_Elapsed);

            TheTimer.Enabled = startNow;
        }

        public void TheTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // test case
            Console.WriteLine(timerCount);
            timerCount++;
        }

        public void StartTimer()
        {
            TheTimer.Enabled = true;
        }

        public void StopTimer()
        {
            TheTimer.Enabled = false;
        }
    }
}
Now: let's test in a Form:
C#
// assume you have created a WinForms Project
// and that it has the standard default Form, Form1
//
// requires at least these libraries:
using System;
using System.Windows.Forms;
using System.Threading;
//
namespace TestTimerInClass_Form
{
    public partial class Form1
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            // note because we used a separate NameSpace
            // the fully qualified name of the class, which must include
            // the NameSpace must be used
            ClassWithTimer.TimerInClass InstanceOfTimer = new ClassWithTimer.TimerInClass(10, false);
        
            InstanceOfTimer.StartTimer();
        
            for(int x = 0; x < 100; x++)
            {
                // let's waste some time
                Thread.Sleep(10);
            }
        
            InstanceOfTimer.StopTimer();
        }
    }
}
Run this code, and examine the results in the Output Window in Visual Studio to verify the Timer is being called repeatedly.

... edit #1 ... Feb. 18, 2012 ... added the bracketing of the NameSpace of the Form test-bed, and 'partial Form1 bracketing, so the reason for using the fully qualified name of the Timer class in another NameSpace is more clear, and the code is hopefully more readable to a "new user."

... edit #2 ... Feb. 18, 2012 ... added setting the 'AutoReset Property of the System.Timer tp 'true ... even though this is not necessary since it's default value is true: just thought it might increase the "educational value" of the answer.
   
v4
Comments
jaideepsinh 18-Feb-12 2:13am
   
I have only .cs file not form's cs file.I initialize and generate tick event but not call tick event by timer that's problem.Here is my code:
tmr_rec = new Timer();
tmr_rec.Start();
tmr_rec.Interval = 1;
tmr_rec.Tick += new EventHandler(tmr_rec_Tick);

void tmr_rec_Tick(object sender, EventArgs e)
{
tmr_rec.Stop();
}
BillWoodruff 18-Feb-12 2:38am
   
In your code above you start the Windows Forms Timer before you set its 'Interval:" that's a mistake: it will use the default interval setting: set the Interval Property before you start it !

You can make the Timer from the System.Timer library execute "one-time-only" by setting its AutoReset Property to 'false (it is 'true by default when a new System.Timer is created).

If you examine my answer carefully you will see I am using a Timer from the System.Timer library; I am not using a Windows Forms Control.

You specifically asked for a solution that did not use the Windows Forms Timer Control.

With a System.Timer instance there is no 'Tick Event: the corresponding Event is 'Elapsed.

You can use the class defined in my answer from any other class; I simply put it in a WinForm test case, for your benefit, so you can see it working. There are many benefits to using the Timer in the System.Timer library: I suggest you read:

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
something like this?
C#
using System;
using System.Windows.Forms;

namespace TestCSharp
{
    public class MyClass
    {
        Timer _myTimer;


        public void New()
        {
            _myTimer = new Timer();
            _myTimer.Interval = 1000;
            _myTimer.Tick += _myTimer_Tick;
            _myTimer.Start();
        }

        void _myTimer_Tick(object sender, EventArgs e)
        {
            // Do something
        }
    }
}
   
Comments
jaideepsinh 17-Feb-12 2:23am
   
Thank' for replay.But by this code tick event not call by the timer.
I am using timer control in only cs file not in window form.
lukeer 17-Feb-12 5:17am
   
You need to create an instance of the above class and call the New() method. If you haven't done that, you can use the following code somewhere in your project
MyClass myClassInstance = new MyClass();
myClassInstance.New();
jaideepsinh 18-Feb-12 2:13am
   
I have only .cs file not form's cs file.I initialize and generate tick event but not call tick event by timer that's problem.Here is my code:
I am start timer in serial port data receive event but when i start timer in other method then it will work proper.
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
tmr_rec = new Timer();
tmr_rec.Start();
tmr_rec.Interval = 1;
tmr_rec.Tick += new EventHandler(tmr_rec_Tick);
}
void tmr_rec_Tick(object sender, EventArgs e)
{
tmr_rec.Stop();
}

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