/* Prepares and starts the desired timer */ private Timer CreateTimer(Handler handler, int messageType, int period) { // Create the object which links timer and request PeriodicCommandRequest speedPeriodicRequest = new PeriodicCommandRequest(handler, messageType); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(ManageTimer); // Create a timer that waits one second, then invokes every second. return(new Timer(timerDelegate, speedPeriodicRequest, 1000, period)); }
/* Called by the timer delegate when timer expires */ private void ManageTimer(object state) { PeriodicCommandRequest ts = (PeriodicCommandRequest)state; ts.handler.ObtainMessage(ts.messageType, -1, -1).SendToTarget(); }