0

I have successfully used the solution stated in this question.

I now want to reuse the Timer Methods in Several Different Forms and have been trying to create a class so as to keep the number of methods in my various forms to a minimum. However I am fairly new to programming and using Classes and Events and don't quite think I am approaching this correctly.

Below is my class so far. Can and event in my class trigger and event in the main program? Should I even be using a class?

using System;
using System.Timers;

namespace TMP_ERP
{
    class Timer
    {
        int MilliSeconds;
        System.Windows.Forms.Timer queryTimer;

        public Timer()
        {
        }


        //Revokes the timer if not already revoked
        public void RevokeQueryTimer()
        {
            if (queryTimer != null)
            {
                queryTimer.Stop();
                queryTimer.Tick -= queryTimer_Tick;
                queryTimer = null;
            }
        }

        public void RestartQueryTimer()
        {
            //start or reset a pending query
            if (queryTimer == null)
            {
                queryTimer = new System.Windows.Forms.Timer { Enabled = true, Interval     = 1500 };
                queryTimer.Tick += queryTimer_Tick;
            }

            else
            {
                queryTimer.Stop();
                queryTimer.Start();
            }
        }

        void queryTimer_Tick(object sender, EventArgs e)
        {
            //Tells Main Program to do something?
        }
    }
}

Each time someone types a character I would call RestartQueryTimer(). If the QueryTimer ticks than I am query a database and then call RevokeQueryTimer() to remove the timer.

Community
  • 1
  • 1
Matt
  • 358
  • 3
  • 9
  • 23
  • You would have to raise your own tick event and have your listener subscribe to that event. This Timer class of yours shouldn't know anything about your Main form. It's not very clear what advantage your class has over just using the timer component on your form. – LarsTech Aug 13 '13 at 21:42
  • I am planning on having several WindowsForms in one project solution and am trying to avoid adding these methods to each form I create. (Just less code to be scrolling past constantly). Is this considered a Weak Event Pattern described by this [article](http://msdn.microsoft.com/en-us/library/aa970850.aspx)? – Matt Aug 13 '13 at 21:51
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Aug 13 '13 at 22:12

1 Answers1

0

The best example you can have in this situation is the interface of the timer you're already using inside your Timer - the System.Windows.Forms.Timer. Provide an event to notify the client of your class exactly as the internal Timer does.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • Would this be referred to as a callback? – Matt Aug 13 '13 at 22:48
  • Usually this term is used in the context of providing a pointer to function which a class calls for the client, or a provided callback interface. In the context of events usually only the term 'event' is used and is commonly recognized. But technically it's almost the same thing. The main difference is that events are sometimes handled in the context of an event loop, so they are something more than ordinary callbacks. – BartoszKP Aug 13 '13 at 22:54