PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/timeouts.md

http://github.com/jacksonh/manos
Markdown | 78 lines | 54 code | 24 blank | 0 comment | 0 complexity | 94394fa2b26d9ebba2de6ce865d55c04 MD5 | raw file
 1Timeouts in Manos
 2=================
 3
 4Manos allows for tasks to be scheduled using its timeout framework.  These tasks are performed on the main thread, in Manos' main IO loop, so they shouldn't be used for long running operations. Timeouts are mostly useful for small tasks that are time based, like flushing a cache after a certain amount of time.
 5
 6Timeouts are not guaranteed to start at an exact time, but they will always wait until the specified amount of time has expired before running.
 7
 8
 9Here is a simple example of a timeout:
10
11    MyManosApp ()
12    {
13        AddTimeout (TimeSpan.FromMinutes (5), app => app.Cache.Clear ());
14    }
15
16The previous example would be executed just once, if we want to clear our cache every five minutes we can use a RepeatBehavior:
17
18    MyManosApp ()
19    {
20        AddTimeout (TimeSpan.FromMinutes (5), RepeatBehavior.Forever, app => app.Cache.Clear ());
21    }
22
23The RepeatBehavior class also allows you to specify the number of times to perform a task using the Iterations methods:
24
25    MyManosApp ()
26    {
27        AddTimeout (TimeSpan.FromMinutes (5), RepeatBehavior.Iterations (10), app => app.Cache.Clear ());
28    }
29
30Timeout Callbacks
31-----------------
32The timeout callback is a delegate that will pass the MangoApp and the data object it was added with to your handler. If no data object is used when the timeout was added, data will be null.
33
34    MyManosApp ()
35    {
36        MyData data = new MyData ();
37        
38        AddTimeout (TimeSpan.FromSeconds (10), FooHandler, data);
39    }
40
41    void FooHandler (ManosApp app, object data)
42    {
43        MyData mydata = data as MyData;
44    }
45
46
47Custom RepeatBehaviors
48----------------------
49
50If your application needs a RepeatBehavior more sophisticated than once, forever, or number of iterations you can create a custom RepeatBehavior.  This is done by implementing the IRepeatBehavior interface.  This interface has a two methods ShouldContinueToRepeat, and RepeatPerformed.  The RepeatPerformed notifies the RepeatBehavior that a Repetion has been performed. Manos will then call ShouldContinueToRepeat. If ShouldContinueToRepeat returns false, the timeout is stopped.
51
52Here is a simple example that will clear a cache every five minutes until November 6th 2001.
53
54    class RepeatUntilTime : IRepeatBehavior {
55
56        public DateTime Time {
57            get;
58            set;
59        }
60
61        public bool ShouldContinueToRepeat (ManosApp app)
62        {
63            return DateTime.Now > Time;
64        }
65
66        public void RepeatPerformed ()
67        {
68        }
69    }
70
71    class MyManosApp : ManosApp {
72
73        public ManosApp ()
74        {
75            AddTimeout (TimeSpan.FromMinutes (5), new RepeatUntilTime (2001, 11, 6), app => app.Cache.Clear ());
76        }
77    }
78