/src/Manos/Manos/Timeout.cs
http://github.com/jacksonh/manos · C# · 99 lines · 44 code · 12 blank · 43 comment · 2 complexity · 9bd40df0ac3d1f9f83d06c12c48395e3 MD5 · raw file
- //
- // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- //
- using System;
- namespace Manos
- {
- /// <summary>
- /// Provides a mechanism for things to happen periodically within a ManosApp.
- /// Timeouts are gauranteed to happen at some moment on or after the TimeSpan specified has ellapsed.
- ///
- /// Timeouts will never run before the specified TimeSpan has elapsed.
- ///
- /// Use the method <see cref="M:Manos.AppHost.AddTimeout"/> method to register each Timeout.
- /// </summary>
- public class Timeout
- {
- internal TimeSpan begin;
- internal TimeSpan span;
- internal IRepeatBehavior repeat;
- internal object data;
- internal TimeoutCallback callback;
- private bool stopped;
- public Timeout (TimeSpan span, IRepeatBehavior repeat, object data, TimeoutCallback callback) : this (TimeSpan.Zero, span, repeat,data, callback)
- {
- }
- public Timeout (TimeSpan begin, TimeSpan span, IRepeatBehavior repeat, object data, TimeoutCallback callback)
- {
- this.begin = begin;
- this.span = span;
- this.repeat = repeat;
- this.data = data;
- this.callback = callback;
- }
-
- /// <summary>
- /// Causes the action specified in the constructor to be executed. Infrastructure.
- /// </summary>
- /// <param name="app">
- /// A <see cref="ManosApp"/>
- /// </param>
- public void Run (ManosApp app)
- {
- if (stopped)
- return;
- try {
- callback (app, data);
- } catch (Exception e) {
- Console.Error.WriteLine ("Exception in timeout callback.");
- Console.Error.WriteLine (e);
- }
-
- repeat.RepeatPerformed ();
- }
- /// <summary>
- /// Stop the current timeout from further execution. Once a timeout is stopped it can not be restarted
- /// </summary>
- public void Stop ()
- {
- stopped = true;
- }
- /// <summary>
- /// Inidicates that the IOLoop should retain this timeout, because it will be run again at some point in the future. Infrastructure.
- /// </summary>
- public bool ShouldContinueToRepeat ()
- {
- return !stopped && repeat.ShouldContinueToRepeat ();
- }
- }
- }