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.