Permalink
Browse files

Add Immediately method to deferred delegates

  • Loading branch information...
1 parent 3c2caee commit 2a5b9f6aa2ba7f710de10887b1f20f90d2b92b9e @cdhowie committed Oct 18, 2012
Showing with 31 additions and 20 deletions.
  1. +31 −20 Cdh.Toolkit.Extensions/Delegates/Defer.cs
@@ -49,6 +49,35 @@ internal DeferState(Action action)
this.action = action;
}
+ private void Execute()
+ {
+ try {
+ action();
+ } catch (Exception ex) {
+ var handled = false;
+
+ foreach (var handler in exceptionHandlers) {
+ handled = handler(ex);
+ if (handled) {
+ break;
+ }
+ }
+
+ if (!handled) {
+ throw;
+ }
+ }
+ }
+
+ public void Immediately()
+ {
+ Action processor = Execute;
+
+ AsyncCallback callback = result => processor.EndInvoke(result);
+
+ processor.BeginInvoke(result => processor.EndInvoke(result), null);
+ }
+
public void For(TimeSpan timeSpan)
{
if (timeSpan.CompareTo(TimeSpan.Zero) <= 0) {
@@ -57,28 +86,10 @@ public void For(TimeSpan timeSpan)
Action processor = delegate {
Thread.Sleep(timeSpan);
-
- try {
- action();
- } catch (Exception ex) {
- var handled = false;
-
- foreach (var handler in exceptionHandlers) {
- handled = handler(ex);
- if (handled) {
- break;
- }
- }
-
- if (!handled) {
- throw;
- }
- }
+ Execute();
};
- AsyncCallback callback = result => processor.EndInvoke(result);
-
- processor.BeginInvoke(callback, null);
+ processor.BeginInvoke(result => processor.EndInvoke(result), null);
}
public void Until(DateTime dateTime)

0 comments on commit 2a5b9f6

Please sign in to comment.