Hello all,
I have a quick question about timers in C#.
I have a System.Windows. Forms.Timer timer (the kind you "drag" out of the toolbox.
It looks something like this:
[CODE=vbnet]
public class Class {
private Timer timer;
private bool flag = false;
public Class() {
timer = new Timer();
timer.Interval = 400;
timer.Tick += new System.EventHan dler(timerHandl er);
}
public void Function() {
timer.Start();
doSomethingWhic hWillTriggerCal lback();
}
private void Callback() {
if (!flag) {
return;
}
flag = false;
doSomething();
}
private void timerHandler() {
flag = true;
timer.Stop();
}
[/CODE]
Periodically a function is called which calls a method in another class which will eventually result an event callback. Sometimes this callback executes almost immediately. What I want is that there is a "safe time" during which the event is ignored.
So, in Function() the timer is started, in 400 ms, flag is set true, and then if the callback executes it does not break out.
Some unspecified amount of time later, Function() is called again (in theory) restarting the timer.
So the idea is that if flag is false (a certain amount of time has not elapsed, it breaks out of the event callback.
Unfortunately, this only works once, after the first time through the timerHandler never gets called, even though timer.Enabled claims to be true.
Any help would be most appreciated.
~ Christopher
I have a quick question about timers in C#.
I have a System.Windows. Forms.Timer timer (the kind you "drag" out of the toolbox.
It looks something like this:
[CODE=vbnet]
public class Class {
private Timer timer;
private bool flag = false;
public Class() {
timer = new Timer();
timer.Interval = 400;
timer.Tick += new System.EventHan dler(timerHandl er);
}
public void Function() {
timer.Start();
doSomethingWhic hWillTriggerCal lback();
}
private void Callback() {
if (!flag) {
return;
}
flag = false;
doSomething();
}
private void timerHandler() {
flag = true;
timer.Stop();
}
[/CODE]
Periodically a function is called which calls a method in another class which will eventually result an event callback. Sometimes this callback executes almost immediately. What I want is that there is a "safe time" during which the event is ignored.
So, in Function() the timer is started, in 400 ms, flag is set true, and then if the callback executes it does not break out.
Some unspecified amount of time later, Function() is called again (in theory) restarting the timer.
So the idea is that if flag is false (a certain amount of time has not elapsed, it breaks out of the event callback.
Unfortunately, this only works once, after the first time through the timerHandler never gets called, even though timer.Enabled claims to be true.
Any help would be most appreciated.
~ Christopher
Comment