Reputation: 1213
This function starts a timer in GTK#. I want to be able to start and stop this as I please.
void StartClock ()
{
GLib.Timeout.Add (1000, new GLib.TimeoutHandler (AskPython));
}
Upvotes: 1
Views: 1959
Reputation: 1032
You can use a global variable to disable that could be checked within AskPython
function and do nothing if it is set.
Otherwise, which I think is the right way for GLib. AskPython
should return false
.
The function is called repeatedly until it returns False, at which point the timeout is automatically destroyed and the function will not be called again.
Reference: glib.timeout_add
Then call GLib.Timeout.Add
if you want to enable it again.
Upvotes: 0
Reputation: 338
Glib timeout doesn't support that but here is a timer class I wrote that mimics Microsoft's timer class.
public delegate void TimerElapsedHandler (object sender, TimerElapsedEventArgs args);
public class TimerElapsedEventArgs : EventArgs
{
DateTime signalTime;
public TimerElapsedEventArgs () {
signalTime = DateTime.Now;
}
}
public class Timer
{
private bool _enabled;
public bool enabled {
get {
return _enabled;
}
set {
_enabled = value;
if (_enabled)
Start ();
else
Stop ();
}
}
protected uint timerId;
public event TimerElapsedHandler TimerElapsedEvent;
public uint timerInterval;
public bool autoReset;
public Timer () : this (0) { }
public Timer (uint timerInterval) {
_enabled = false;
this.timerInterval = timerInterval;
autoReset = true;
timerId = 0;
}
public void Start () {
_enabled = true;
timerId = GLib.Timeout.Add (timerInterval, OnTimeout);
}
public void Stop () {
_enabled = false;
GLib.Source.Remove (timerId);
}
protected bool OnTimeout () {
if (_enabled) {
if (TimerElapsedEvent != null)
TimerElapsedEvent (this, new TimerElapsedEventArgs ());
}
return _enabled & autoReset;
}
}
Usage:
Timer t = new Timer (1000);
t.TimerElapsedEvent += (sender, args) => {
Console.WriteLine (args.signalTime.ToString ());
};
t.enabled = true;
Upvotes: 2