ive been working on a program. i has 3 classes. 2 of the classes have timers that repeat at different intervals and once one "cycle" of the timer is done it raises an event with a string as return. the 3rd class subscribes to the events from the other two timer classes and prints them to screen. it works great!
but my issue is that it prints them separately. say currently the first timer class runs and then raises "hello" every 2 minutes and the other class "dog" every second. then every time an event is raised it prints the raised event to console. i would want it to instead print "hellodog" every second.
i was thinking: so each time a timer fires it will raise an event and update a string in the "output" class with the current value, then make another timer that goes off every second, this timer will read both the updated strings together as one output like "hellodog". is this possible if it is this is the easiest way i think. how would i achieve this idea?
if it is confusing i will clarify.
namespace Final
{
public class Output
{
public static void Main()
{
var timer1 = new FormWithTimer();
var timer2 = new FormWithTimer2();
timer1.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable);
timer2.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer2_NewStringAvailable);
Console.ReadLine();
}
static void timer1_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e)
{
var theString = e.Value;
//To something with 'theString' that came from timer 1
Console.WriteLine(theString);
}
static void timer2_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e)
{
var theString2 = e.Value;
//To something with 'theString2' that came from timer 2
Console.WriteLine(theString2);
}
}
public abstract class BaseClassThatCanRaiseEvent
{
public class StringEventArgs : EventArgs
{
public StringEventArgs(string value)
{
Value = value;
}
public string Value { get; private set; }
}
//The event itself that people can subscribe to
public event EventHandler<StringEventArgs> NewStringAvailable;
protected void RaiseEvent(string value)
{
var e = NewStringAvailable;
if (e != null)
e(this, new StringEventArgs(value));
}
}
public partial class FormWithTimer : BaseClassThatCanRaiseEvent
{
Timer timer = new Timer();
public FormWithTimer()
{
timer = new System.Timers.Timer(200000);
timer.Elapsed += new ElapsedEventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (200000); // Timer will tick evert 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick(object sender, EventArgs e)
{
...
RaiseEvent(gml.ToString());
}
}
public partial class FormWithTimer2 : BaseClassThatCanRaiseEvent
{
Timer timer = new Timer();
public FormWithTimer2()
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Tick2); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000); // Timer will tick evert 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick2(object sender, EventArgs e)
{
...
RaiseEvent(aida.ToString());
}
}
}
You can use the same event handler for both timers. And construct the output by identifying the senders. (Didn't test the code for syntax errors.)
private static string timer1Value = string.Empty;
private static string timer2Value = string.Empty;
private static FormWithTimer timer1;
private static FormWithTimer2 timer2;
public static void Main()
{
timer1 = new FormWithTimer();
timer2 = new FormWithTimer2();
timer1.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable);
timer2.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable);
Console.ReadLine();
}
static void timer1_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e)
{
if (sender == timer1)
{
timer1Value = e.Value.ToString();
}
else if (sender == timer2)
{
timer2Value = e.Value.ToString();
}
if (timer1Value != String.Empty && timer2Value != String.Empty)
{
Console.WriteLine(timer1Value + timer2Value);
// Do the string concatenation as you want.
}
When the events are handled in your example they have no access to information about the other events. If you want to have 2 events that update strings, but you want the handler to print data from both updated strings, you need the event handlers to have access to both of those strings. You can either store them in variables on the event handling class, or make them public properties of the classes that are raising the events. That way in either event handler you have access to the updated strings from other events.
Related
I am new to C# and try to create a setInterval and clearInterval function which is exactly working like the same functions in javascript.
I do it mainly for practice and to learn what can be done in C# and what not.
setInterval
Requirements: Create a new timer and return it, also run a anonymous function or a predefined function again and again in the given interval.
Timer setInterval(Func<int> myMethod, int intervalInMs)
{
var timer = new Timer();
timer.Start();
while (true) { //probably a infinite loop
if (timer.ElapsedMilliseconds >= intervalInMs)
{
myMethod();
timer.Restart();
}
}
return timer; //Code does never reach this part obviously because of the while loop
}
clearInterval
Requirements: Stop the timer.
void clearInterval(Timer timer)
{
timer.Stop();
}
Planned Usage
Timer myTimer = setInterval(delegate {
MessageBox.Show(
"test",
"test",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
return 1;
}, 5000);
//somewhere in code...
clearInterval(myTimer);
How is it possible to solve this with C#, by using Events?
The .Net framework provides at least three different timers - System.Timers.Timer, System.Threading.Timer and System.Windows.Forms.Timer.
The System.Diagnostic.Stopwatch is not a timer and should not be used as one. It only measures how much time have passed between the Start() and Stop().
I would suggest using one of the timers provided by the .Net framework instead of re-inventing the wheel.
update
Well, since you insisted,
Here is a simple implementation NOT FOR USE IN PRODUCTION CODE since it's very easy to create memory leaks with it:
public static class Interval
{
public static System.Timers.Timer Set(System.Action action, int interval)
{
var timer = new System.Timers.Timer(interval);
timer.Elapsed += (s, e) => {
timer.Enabled = false;
action();
timer.Enabled = true;
};
timer.Enabled = true;
return timer;
}
public static void Stop(System.Timers.Timer timer)
{
timer.Stop();
timer.Dispose();
}
}
You can see a live demo on rextester.
I managed to solve it by using events. It is maybe not the best solution, but as I already mentioned, I am a C# beginner.
Example:
This example shows a message box every 5000 ms, after three Messageboxes the interval is changed and at 10 boxes the interval is cleared. You can also stop the interval by pressing the button.
using System;
using System.Windows.Forms;
namespace Intervals
{
public partial class Form1 : Form
{
Interval ival1 = new Interval();
private int _A;
public int A
{
get { return _A; }
set {
_A = value;
if (value == 3) {
if (ival1 != null) {
MessageBox.Show(
"changeInterval Triggered",
A.ToString(),
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
ival1.changeInterval(1000);
}
}
if (value >= 10)
{
if (ival1 != null)
{
MessageBox.Show(
"clearInterval Triggered",
A.ToString(),
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
ival1.clearInterval();
}
}
}
}
public Form1()
{
InitializeComponent();
int interval = 5000;
ival1.setInterval(delegate {
A++;
MessageBox.Show(
A.ToString(),
"A",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
}, interval);
}
private void stopInterval_Click(object sender, EventArgs e)
{
ival1.clearInterval();
}
}
}
I created a new class Interval which is needed in order for this program to work.
using System;
namespace Intervals
{
public class Interval
{
private System.Timers.Timer timer;
private Action main;
public Interval()
{
timer = new System.Timers.Timer();
}
public void setInterval(Action pAction, int interval)
{
if (interval <= 0) { interval = 100; }
timer.Interval = interval;
main = new Action(delegate{
timer.Stop();
pAction?.Invoke();
timer.Start();
});
timer.Elapsed += (sender, e) => TimerEventProcessor(sender, e);
timer.Start();
}
public void changeInterval(int interval)
{
timer.Stop();
timer.Interval = interval;
timer.Start();
}
public void clearInterval()
{
main?.EndInvoke(null);
main = delegate
{
timer.Stop();
timer.Dispose();
};
}
public void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
main?.Invoke();
}
}
}
Memory Usage Test (260 Minutes):
I have my winforms built in timer:
System.Windows.Forms.Timer timerLoop;
When the timer is started, I want (if possible) to pass this timer an integer value.
timerLoop.Start();
Till now, I only created a general variable that the timer can read and update this variable just before starting my timer.
You can do this two ways (maybe more):
Extend base Timer by creating new one that inherit it:
private class TimerExnteded : Timer
{
public int Value { get; set; }
public TimerExnteded(int value)
{
Value = value;
}
}
and use that value in Tick event.
Use Tag property of Timer
Timer t = new Timer();
t.Tag = 5;
t.Start();
//event
private void t_Tick(object sender, EventArgs e)
{
var timer = sender as Timer;
var value = (timer.Tag as int?) ?? 0;
value++;
timer.Tag = value;
}
Second approach uses boxing/unboxing of value.
You can also use closure:
t.Tick += (s, a) => OnTick(YourValue);
I have event handler:
private void Control_Scroll(object sender, ScrollEventArgs e)
{
UpdateAnnotations();
}
Now I wish to update annotations only if user stopped scrolling, like if since last scrolling event passed 100ms, then execute action, else discard it, as it won't matter anyway.
What would be the easiest/reusable way to do that, preferably some static method like public static void DelayedAction(Action action, TimeSpan delay).
Using .NET 4.0.
See this answer to an Rx (Reactive Extensions) question. (You can use Observable.FromEvent to create an observable from an event.)
I would go with something like this
class MyClass
{
private System.Timers.Timer _ScrollTimer;
public MyClass()
{
_ScrollTimer= new System.Timers.Timer(100);
_ScrollTimer.Elapsed += new ElapsedEventHandler(ScrollTimerElapsed);
}
private void ResetTimer()
{
_ScrollTimer.Stop();
_ScrollTimer.Start();
}
private void Control_Scroll(object sender, ScrollEventArgs e, TimeSpan delay)
{
ResetTimer();
}
private void ScrollTimerElapsed(object sender, ElapsedEventArgs e)
{
_ScrollTimer.Stop();
UpdateAnnotations();
}
}
Every time the user scrolls, the timer gets reset and only when scrolling stops for 100ms the TimerElapsed gets fired and you can update your annotations.
I tried this with several controls on the form at the same time, and it is reusable by outside.
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
if (DelayedAction(100, sender))
UpdateAnnotations();
}
Dictionary<object, Timer> timers = new Dictionary<object, Timer>();
bool DelayedAction(int delay, object o)
{
if (timers.ContainsKey(o))
return false;
var timer = new Timer();
timer.Interval = delay;
timer.Tick += (s, e) =>
{
timer.Stop();
timer.Dispose();
lock(timers)
timers.Remove(o);
};
lock(timers)
timers.Add(o, timer);
timer.Start();
return true;
}
The dictionary is locked, because if a user cannot hit two controls at the same time, a timer might be inserted at the same time as another one is removed.
Try this class:
public class ActionHelper
{
private static Dictionary<Delegate, System.Threading.Timer> timers =
new Dictionary<Delegate, System.Threading.Timer>();
private static object lockObject = new object();
public static void DelayAction(Action action, TimeSpan delay)
{
lock (lockObject)
{
System.Threading.Timer timer;
if (!timers.TryGetValue(action, out timer))
{
timer = new System.Threading.Timer(EventTimerCallback, action,
System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
timers.Add(action, timer);
}
timer.Change(delay, TimeSpan.FromMilliseconds(-1));
}
}
public static void EventTimerCallback(object state)
{
var action = (Action)state;
lock (lockObject)
{
var timer = timers[action];
timers.Remove(action);
timer.Dispose();
}
action();
}
}
Features:
Thead safe
Supports multiple concurrent actions
Usage:
private void Control_Scroll(object sender, ScrollEventArgs e)
{
ActionHelper.DelayAction(UpdateAnnotations, TimeSpan.FromSeconds(1));
}
Just be aware that the method is called in a separate thread. If you need to do UI work, you need to use Control.Invoke (WinForms) or Dispatcher.Invoke (WPF):
// The method is contained in a Form (winforms)
private void UpdateAnnotations()
{
if (this.InvokeRequired)
this.Invoke(new Action(UpdateAnnotations));
else
{
MessageBox.Show("Method is called");
}
}
Could you not store the time the event was fired (DateTime.Now) and when ever it's called check how long it's been since the last time (e.g. DateTime.Now - lastExecutionTime > minTime)
** Update **
Or a more generic way based on your static helper idea:
public static void DelayedAction(Action action, TimeSpan delay)
{
var delayedActionTimer = new Timer(x => action(), null, delay, TimeSpan.FromMilliseconds(-1));
}
Needs work obviously... for instance you could store the timer in a field and reset (change) the delay each time the user scrolls
I'm attempting to implement the MSDN example (http://msdn.microsoft.com/en-us/library/swx5easy.aspx) for Thread.Timers in my own code.
I want to be able to cancel the timer when a certain user action is performed, however I can not dispose the timer, I suspect this is because I'm calling a method from another class so I need to adjust; but I don't know where.
Other than this, the timer works fine. Can anyone see why my timer will not cancel when btnconfigOpenConfig is called?
FYI I'm converting what was a worker process to a timed event.
public partial class Xservt : Window
{
internal class TimerStateObjClass
{
public int SomeValue;
public System.Threading.Timer SqlUpdateFromTwitterTimerReference;
public bool TimerCanceled;
}
internal void SomeMethod(){
TimerStateObjClass stateObj = new TimerStateObjClass();
stateObj.TimerCanceled = false;
stateObj.SomeValue = 100;
System.Threading.TimerCallback timerDelegate =
new System.Threading.TimerCallback(twit.hometimelineclass._sqlUpdateFromTwitterWorker_DoWork);
var sqlUpdateFromTwitterTimer = new Timer(timerDelegate, stateObj, 0,20000);
stateObj.SqlUpdateFromTwitterTimerReference = sqlUpdateFromTwitterTimer;
}
}
//action to perform which disposes the timer
private void btnconfigOpenConfig(object sender, RoutedEventArgs e)
{
TimerStateObjClass timerState = new TimerStateObjClass();
timerState.TimerCanceled = true;
}
//Actions the timer is calling, in another class
internal static void _sqlUpdateFromTwitterWorker_DoWork(object StateObj)
{
Xservt.TimerStateObjClass state = (Xservt.TimerStateObjClass) StateObj;
if(state.TimerCanceled)
{
state.SqlUpdateFromTwitterTimerReference.Dispose();
}
//some work
}
As Hans pointed out in the comments, you need to keep a reference to TimerStateObjClass you originally created. You can then use that to set TimerCanceled.
public partial class Xservt : Window
{
internal class TimerStateObjClass
{
public int SomeValue;
public System.Threading.Timer SqlUpdateFromTwitterTimerReference;
public bool TimerCanceled;
}
TimerStateObjClass stateObj; //THIS IS THE ORIGINAL STATE OBJ
internal void SomeMethod()
{
stateObj = new TimerStateObjClass();
stateObj.TimerCanceled = false;
stateObj.SomeValue = 100;
System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(twit.hometimelineclass._sqlUpdateFromTwitterWorker_DoWork);
var sqlUpdateFromTwitterTimer = new Timer(timerDelegate, stateObj, 0, 20000);
stateObj.SqlUpdateFromTwitterTimerReference = sqlUpdateFromTwitterTimer;
}
//action to perform which disposes the timer
private void btnconfigOpenConfig(object sender, RoutedEventArgs e)
{
//HERE WE CAN GET AT THE ORIGINAL STATE OBJ
stateObj.TimerCanceled = true;
}
}
//Actions the timer is calling, in another class
internal static void _sqlUpdateFromTwitterWorker_DoWork(object StateObj)
{
Xservt.TimerStateObjClass state = (Xservt.TimerStateObjClass)StateObj;
if (state.TimerCanceled)
{
state.SqlUpdateFromTwitterTimerReference.Dispose();
}
//some work
}
You need to store reference to your timer (or class that references the timer) somewhere in your class.
To stop the timer there is not need to dispose it. You can just call timer.Change(-1, -1);. That will allow to re-enable timer again by calling timer.Change(dueTimeInMs, intervalInMs);
You code should be something like that:
public partial class Xservt : Window
{
private Timer timer = new Timer(o => DoSomething());
private void StartTimer()
{
var period = 5 * 1000; // 5 sec
timer.Change(0, period);
}
private void StopTimer()
{
timer.Change(-1, -1);
}
}
Then call StartTimer to run it and StopTimer to stop respectively.
Also note that if there is any chance that DoSomething will run longer than timer interval that would result in running that method in more than one thread concurrently. To avoid that DO NOT use Timer's interval but use dueTime instead:
private Timer timer = new Timer(o => {
DoSomething();
StartTimer();
});
private void StartTimer()
{
var period = 5 * 1000; // 5 sec
timer.Change(period, 0);
}
In this timer is trigrered to run only once but after each run it gets re-triggered.
I have a WPF user control called TimerUserControl where contains a timer. And I have another user control where show questions, this one has a NextQuestion function.
The timer has 2 minutes like an interval, and I'd like to invoke the NextQuestion function when it has done. I think I have to use delegates, but I'm not sure.
UPDATE 1:
public partial class TimeUserControl : UserControl
{
public int _totalSeconds;
public int _secondsRemaining;
public DispatcherTimer timerSecondsLeft;
public TimeUserControl()
{
InitializeComponent();
timerSecondsLeft = new DispatcherTimer();
timerSecondsLeft.Tick += new EventHandler(timerSecondsLeft_Tick);
timerSecondsLeft.Interval = new TimeSpan(0, 0, 1);
}
public bool TimesUp
{
get;
set;
}
public void SetSeconds(int seconds)
{
timerSecondsLeft.Stop();
if (seconds == 0)
{
TimeTextBlock.Text = "There's no time! Hurray";
}
else
{
_totalSeconds = seconds;
_secondsRemaining = seconds;
TimeTextBlock.Text = string.Format("It remains {0} seconds. Don't take long!", _totalSeconds);
timerSecondsLeft.Start();
}
}
public void timerSecondsLeft_Tick(object sender, EventArgs e)
{
_secondsRemaining--;
if (_secondsRemaining <= 0)
{
timerSecondsLeft.Stop();
TimesUp = true;
TimeTextBlock.Text = "Time's up. Press Enter to next problem.";
// HERE WILL INVOKE NEXTQUESTION FUNCTION
}
else
{
TimeTextBlock.Text = string.Format("It remains {0} seconds. Don't take long!", _secondsRemaining);
}
}
}
Look in the code, the comment is this possible ussing delegates?
So you need to do a few things. You have to add some code to you're user control.
// Declare this outside your usercontrol class
public delegate void TimerExpiredEventHandler(object sender, EventArgs e);
This is what needs to be added to your code for the user control.
public partial class TimerUserControl : UserControl
{
public event TimerExpiredEventHandler Expired;
public void OnExpired(EventArgs e)
{
if (Expired != null)
Expired(this, e);
}
public void timerSecondsLeft_Tick(object sender, EventArgs e)
{
_secondsRemaining--;
if (_secondsRemaining <= 0)
{
timerSecondsLeft.Stop();
TimesUp = true;
TimeTextBlock.Text = "Time's up. Press Enter to next problem.";
// Fire the event here.
OnExpired(EventArgs.Empty);
}
else
{
TimeTextBlock.Text = string.Format("It remains {0} seconds. Don't take long!", _secondsRemaining);
}
}
}
Now you need to subscribe to this event inside whatever is calling this usercontrol in the first place.
public partial class ParentForm : Form
{
private void ParentForm_Load(object sender, EventArgs e)
{
var timer = new TimerUserControl();
//Subscribe to the expired event that we defined above.
timer.Expired += new EventArgs(Timer_Expired);
}
public void Timer_Expired(object sender, EventArgs e)
{
//Handle the timer expiring here. Sounds like you are calling another function, so do that here.
}
}
Use the TreeHelper to hunt up the tree for a shared Parent and then down the tree for the User Control you want. Something like this pseudo code:
this.Timer = new System.Windows.Threading.DispatcherTimer
{
Interval = new TimeSpan(0, 0, 1)
};
this.Timer.Tick += (s, e) =>
{
var _Control = s as MyFirstControl;
var _Other = LogicalTreeHelper.GetChildren(_Control.Parent)
.Cast<FrameworkElement>().Where(x => x.Name == "FindIt")
.First<MySecondControl>();
_Other.DoMethod();
};
Best of luck!
i would probably break out the functionality of the timer control here; something like this (note: i am writing this on-the-fly so let me know if it doesn't work as-is, and i will help correct any issues):
// a simple delegate to report the amount of time remaining
// prior to the expiration of the major tick interval; zero
// indicates that this major tick has elapsed.
public delegate void delegateMajorMinorTimerTick
(
int TimeRemaining_sec, ref bool CancelTimer
);
// you could use milliseconds for the interval settings to get
// better granularity, or you could switch to setting the major
// interval instead, however that approach would require a bit
// more checking to make sure the control has sane settings.
public class MajorMinorTimer
{
// this sets the interval in seconds between the
// "minor" ticks used for intermediate processing
// these are the "inner" intervals of the timer
private int myMinorTickInterval_sec;
public int MinorTickInterval_sec
{
get { return myMinorTickInterval_sec; }
}
// this sets the number of minor ticks between the
// expiration of the major interval of the timer.
// the "outer" interval of the timer
private int myMinorTicksPerMajorTick;
public int MinorTicksPerMajorTick
{
get { return myMinorTicksPerMajorTick; }
}
public MajorMinorTimer
(
int parMinorTickInterval_sec,
int parMinorTicksPerMajorTick
)
{
MinorTickInterval_sec = parMinorTickInterval_sec;
MinorTicksPerMajorTick = parMinorTicksPerMajorTick;
}
private DispatcherTimer myBackingTimer;
private int myMinorTickCount;
public void Start()
{
// reset the minor tick count and start the dispatcher
// timer with some reasonable defaults.
myMinorTickCount = 0;
myBackingTimer =
new DispatcherTimer
(
TimeSpan.FromSeconds(MinorTickInterval_sec),
DispatcherPriority.Normal,
new EventHandler(myBackingTimer_Tick),
Dispatcher.CurrentDispatcher
);
myBackingTimer.Start();
}
public event delegateMajorMinorTimerTick onTick;
private bool FireMajorMinorTimerTick(int TimeRemaining_sec)
{
// allows the timer sink to cancel the timer after this
// call; just as an idea, also could be handled with a
// call to Stop() during the event, but this
// simplifies handling a bit (at least to my tastes)
bool CancelTimer = false;
if (onTick != null)
onTick(TimeRemaining_sec, ref CancelTimer);
return CancelTimer;
}
private void myBackingTimer_Tick(object Sender, EventArgs e)
{
// since we are using a DispatchTimer with settings that should
// do not suggest the possibility of synchronization issues,
// we do not provide further thread safety. this could be
// accomplished in the future if necessary with a lock() call or
// Mutex, among other methods.
++myMinorTickCount;
int TicksRemaining = myMinorTickCount - MinorTicksPerMajorTick;
bool Cancel =
FireMajorMinorTimerTick(TicksRemaining * MinorTickInterval_sec);
if (TicksRemaining == 0)
myMinorTickCount = 0;
if (Cancel)
Stop();
}
public void Stop()
{
myBackingTimer.Stop();
}
}
then, assuming, say, a Quiz control, the timer is used like so:
public void QuestionTimerSetup()
{
// sets up a timer to fire a minor tick every second
// with a major interval of 5 seconds
MajorMinorTimer timerQuestion = new MajorMinorTimer(1, 5);
timerQuestion.onTick +=
new delegateMajorMinorTimerTick(QuestionControl_QuestionTimerTick);
}
// ...
public void QuestionControl_OnTick(int TimeRemaining_sec, ref bool CancelTimer)
{
if (TimeRemaining_sec > 0)
{
tblockQuizStatus.Text =
string.Format("There are {0} seconds remaining.", TimeRemaining_sec);
}
else
{
// just for an example
if (NoMoreQuestions)
{
CancelTimer = true;
HandleEndOfQuiz();
tblockQuizStatus.Text =
"Time's up! The quiz is complete!";
}
else
{
tblockQuizStatus.Text =
"Time's up! Press Enter to continue to the next problem.";
}
}
}
another option (rather than, or in addition to, events) in implementing this might be to add an Action taking the time remaining in the major interval for the minor interval action, an Action for the major interval action, and a Func<bool> that checks the stop condition, allowing the user to perform the desired actions in that way. like this:
public class MajorMinorTimer
{
public MajorMinorTimer
(
int parMinorTimerInterval_sec,
int parMinorTicksPerMajorTick,
Action<int> parMinorTickAction,
Action parMajorTickAction,
Func<bool> parShouldStopFunc
)
{
myMinorTimerInterval_sec = parMinorTimerInterval_sec;
myMinorTicksPerMajorTick = parMinorTicksPerMajorTick;
myMinorTickAction = parMinorTickAction;
myMajorTickAction = parMajorTickAction;
myShouldStopFunc = parShouldStopFunc;
}
private Action<int> myMinorTickAction;
private Action myMajorTickAction;
private Func<bool> myShouldStopFunc;
private void myBackingTimer_OnTick()
{
++myMinorTickCount;
int TicksRemaining = myMinorTickCount - MinorTicksPerMajorTick;
if (TicksRemaining == 0)
myMajorTickAction();
else
myMinorTickAction(TicksRemaining * MinorTickInterval_sec);
bool Cancel = myShouldStopFunc();
if (TicksRemaining == 0)
myMinorTickCount = 0;
if (Cancel)
Stop();
}
}
and then in the quiz code instead of hooking up the event do something like:
public void QuestionTimerSetup()
{
MajorMinorTimer timerQuestion =
new MajorMinorTimer
(
1,
5,
// major interval action
(SecsRemaining) =>
{
tblockQuizStatus.Text =
string.Format
(
"There are {0} seconds remaining.", SecsRemaining
);
},
// minor interval action
() =>
{
if (NoMoreQuestions)
{
tblockQuizStatus.Text =
"Time's up! This completes the quiz!";
HandleEndOfQuiz();
}
else
{
tblockQuizStatus.Text =
"Time's up! Press Enter to continue to next question.";
}
},
// timer cancel check function
() =>
IsEndOfQuizHandled()
);
}