1

What I need is to have a timer fire an event handler (say every second) which is in another class. This will be a small part of a Windows form program.

I have tried using a delegate to "call" an event handler, but I keep getting syntax errors. Can somebody steer me in the correct direction with a simple code example?

The code below is my start, the commented portion works fine but I want the event to fire when a Windows timer fires.

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

        public event TimerHandler Tick;
        public EventArgs e = null;
        public delegate void TimerHandler(Timer t, EventArgs e);

        public class Timer
        {
            public event TimerHandler Tick;
            public EventArgs e = null;
            public delegate void TimerHandler(Timer t, EventArgs e);
        }

        public class Listener
        {
            public static int ticker = 0;
            public void Subscribe(Timer t)
            {
                t.Tick += new Timer.TimerHandler(HeardTick);
            }
            private void HeardTick(Timer t, EventArgs e)
            {
                //lblTimer.Text = ticker.ToString(); //Don't know how to change forms control
                ticker++;
            }
        }

        private void btnStart_Click_1(object sender, EventArgs e)
        {
            Timer t = new Timer();
            Listener l = new Listener();
            l.Subscribe(t);
            //t.Start();
        }

        public void timer1_Tick(object sender, EventArgs e)
        {
            if (Tick != null)
            {
                Tick(this, e); // "this" is incorrect, invalid argument
            }
        }
    }
}
5
  • 2
    show us your code!We would help you correct it.. Commented Oct 7, 2013 at 3:18
  • 2
    Close-Voting: Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. Commented Oct 7, 2013 at 3:18
  • Perhaps give the newbie a chance to edit his question before closing? :) Commented Oct 7, 2013 at 3:50
  • Remember when asking questions to specify what you have tried. This will help you get the answers you need. Commented Oct 7, 2013 at 19:03
  • @JunWeiLee The point of closing a question is to give the author time to edit the question; if they are able to improve it then it can be reopened without the answer being harmed by low quality answers posted before it was in an answerable state. Leaving a question open when it's not ready to be answers is more harmful to the author than closing it quickly. Commented Oct 7, 2013 at 19:44

1 Answer 1

1

Is the other class static? Here is an example for each:

//Static class
Timer1.Tick += YourClass.DoStuff;

//Non-static class
YourClass MyInstance = new YourClass();
Timer1.Tick += MyInstance.DoStuff;

Just put the code in the constructor of your form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.