Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add Immediately method to deferred delegates

  • Loading branch information...
commit 2a5b9f6aa2ba7f710de10887b1f20f90d2b92b9e 1 parent 3c2caee
Chris Howie authored October 18, 2012

Showing 1 changed file with 31 additions and 20 deletions. Show diff stats Hide diff stats

  1. 51  Cdh.Toolkit.Extensions/Delegates/Defer.cs
51  Cdh.Toolkit.Extensions/Delegates/Defer.cs
@@ -49,6 +49,35 @@ internal DeferState(Action action)
49 49
                 this.action = action;
50 50
             }
51 51
 
  52
+            private void Execute()
  53
+            {
  54
+                try {
  55
+                    action();
  56
+                } catch (Exception ex) {
  57
+                    var handled = false;
  58
+                    
  59
+                    foreach (var handler in exceptionHandlers) {
  60
+                        handled = handler(ex);
  61
+                        if (handled) {
  62
+                            break;
  63
+                        }
  64
+                    }
  65
+                    
  66
+                    if (!handled) {
  67
+                        throw;
  68
+                    }
  69
+                }
  70
+            }
  71
+
  72
+            public void Immediately()
  73
+            {
  74
+                Action processor = Execute;
  75
+
  76
+                AsyncCallback callback = result => processor.EndInvoke(result);
  77
+
  78
+                processor.BeginInvoke(result => processor.EndInvoke(result), null);
  79
+            }
  80
+
52 81
             public void For(TimeSpan timeSpan)
53 82
             {
54 83
                 if (timeSpan.CompareTo(TimeSpan.Zero) <= 0) {
@@ -57,28 +86,10 @@ public void For(TimeSpan timeSpan)
57 86
 
58 87
                 Action processor = delegate {
59 88
                     Thread.Sleep(timeSpan);
60  
-
61  
-                    try {
62  
-                        action();
63  
-                    } catch (Exception ex) {
64  
-                        var handled = false;
65  
-
66  
-                        foreach (var handler in exceptionHandlers) {
67  
-                            handled = handler(ex);
68  
-                            if (handled) {
69  
-                                break;
70  
-                            }
71  
-                        }
72  
-
73  
-                        if (!handled) {
74  
-                            throw;
75  
-                        }
76  
-                    }
  89
+                    Execute();
77 90
                 };
78 91
 
79  
-                AsyncCallback callback = result => processor.EndInvoke(result);
80  
-
81  
-                processor.BeginInvoke(callback, null);
  92
+                processor.BeginInvoke(result => processor.EndInvoke(result), null);
82 93
             }
83 94
 
84 95
             public void Until(DateTime dateTime)

0 notes on commit 2a5b9f6

Please sign in to comment.
Something went wrong with that request. Please try again.