Permalink
Browse files

Wait for non stale data

  • Loading branch information...
1 parent 46661e6 commit 4a39cc13891082d7a40d002c04af64f97028bed8 @ppossanzini ppossanzini committed Oct 8, 2013
@@ -34,5 +34,9 @@ public interface ISession
EventCommandStatus Set<T>(T entity) where T : class;
EventCommandStatus SetAll<T>(System.Collections.Generic.IEnumerable<T> entity) where T : class;
EventCommandStatus SetAll<T>(string collection, System.Collections.Generic.IEnumerable<T> entity) where T : class;
+
+ void WaitForNonStaleDataAt(long eventcommandnumber);
+ bool WaitForNonStaleDataAt(long eventcommandnumber, TimeSpan timeout);
+ bool WaitForNonStaleDataAt(long eventcommandnumber, int timeout);
}
}
@@ -21,9 +21,9 @@ public class Session : DeNSo.ISession, IDisposable
private Command _command = new Command();
private Query _query = new Query();
- //private ManualResetEvent _waiting = new ManualResetEvent(false);
- //private long _waitingfor = 0;
- //private long _lastexecutedcommand = 0;
+ private ManualResetEvent _waiting = new ManualResetEvent(false);
+ private long _waitingfor = 0;
+ private long _lastexecutedcommand = 0;
public static string DefaultDataBase { get; set; }
public static Session New { get { return new Session() { DataBase = DefaultDataBase ?? string.Empty }; } }
@@ -47,54 +47,54 @@ public void Dispose()
{
}
- //private void RegisterWaitEventAsync()
- //{
- // Thread waitingThread = new Thread((ThreadStart)delegate
- // {
- // Session.StoreUpdatedHandler += (sn) =>
- // {
- // _lastexecutedcommand = sn;
- // //if (_waitingfor <= sn)
- // _waiting.Set();
- // };
- // });
- // waitingThread.IsBackground = true;
- // waitingThread.Start();
- //}
+ private void RegisterWaitEventAsync()
+ {
+ Thread waitingThread = new Thread((ThreadStart)delegate
+ {
+ Session.StoreUpdatedHandler += (sn) =>
+ {
+ _lastexecutedcommand = sn;
+ //if (_waitingfor <= sn)
+ _waiting.Set();
+ };
+ });
+ waitingThread.IsBackground = true;
+ waitingThread.Start();
+ }
- //#region Wait Methods
- //public void WaitForNonStaleDataAt(long eventcommandnumber)
- //{
- // //if (_lastexecutedcommand >= eventcommandnumber) return;
- // _waitingfor = eventcommandnumber;
- // while (_lastexecutedcommand < eventcommandnumber)
- // {
- // _waiting.WaitOne(200);
- // _waiting.Reset();
- // }
- // _waitingfor = 0;
- //}
- //public bool WaitForNonStaleDataAt(long eventcommandnumber, TimeSpan timeout)
- //{
- // //if (_lastexecutedcommand >= eventcommandnumber) return;
- // _waitingfor = eventcommandnumber;
- // _waiting.WaitOne(timeout);
- // if (_lastexecutedcommand < eventcommandnumber) return false;
- // _waiting.Reset();
- // _waitingfor = 0;
- // return true;
- //}
- //public bool WaitForNonStaleDataAt(long eventcommandnumber, int timeout)
- //{
- // _waitingfor = eventcommandnumber;
+ #region Wait Methods
+ public void WaitForNonStaleDataAt(long eventcommandnumber)
+ {
+ //if (_lastexecutedcommand >= eventcommandnumber) return;
+ _waitingfor = eventcommandnumber;
+ while (_lastexecutedcommand < eventcommandnumber)
+ {
+ _waiting.WaitOne(200);
+ _waiting.Reset();
+ }
+ _waitingfor = 0;
+ }
+ public bool WaitForNonStaleDataAt(long eventcommandnumber, TimeSpan timeout)
+ {
+ //if (_lastexecutedcommand >= eventcommandnumber) return;
+ _waitingfor = eventcommandnumber;
+ _waiting.WaitOne(timeout);
+ if (_lastexecutedcommand < eventcommandnumber) return false;
+ _waiting.Reset();
+ _waitingfor = 0;
+ return true;
+ }
+ public bool WaitForNonStaleDataAt(long eventcommandnumber, int timeout)
+ {
+ _waitingfor = eventcommandnumber;
- // _waiting.WaitOne(timeout);
- // if (_lastexecutedcommand < eventcommandnumber) return false;
- // _waiting.Reset();
- // _waitingfor = 0;
- // return true;
- //}
- //#endregion
+ _waiting.WaitOne(timeout);
+ if (_lastexecutedcommand < eventcommandnumber) return false;
+ _waiting.Reset();
+ _waitingfor = 0;
+ return true;
+ }
+ #endregion
#region Set Methods
public EventCommandStatus Set<T>(T entity) where T : class
@@ -5,21 +5,21 @@
using DeNSo;
-//public static class SessionExtensions
-//{
-// public static void Wait(this EventCommandStatus status)
-// {
-// status._eventSession.WaitForNonStaleDataAt(status.Value);
-// }
+public static class SessionExtensions
+{
+ public static void Wait(this EventCommandStatus status)
+ {
+ status._eventSession.WaitForNonStaleDataAt(status.Value);
+ }
-// public static void Wait(this EventCommandStatus status, TimeSpan timeout)
-// {
-// status._eventSession.WaitForNonStaleDataAt(status.Value, timeout);
-// }
+ public static void Wait(this EventCommandStatus status, TimeSpan timeout)
+ {
+ status._eventSession.WaitForNonStaleDataAt(status.Value, timeout);
+ }
-// public static void Wait(this EventCommandStatus status, int timeout)
-// {
-// status._eventSession.WaitForNonStaleDataAt(status.Value, timeout);
-// }
-//}
+ public static void Wait(this EventCommandStatus status, int timeout)
+ {
+ status._eventSession.WaitForNonStaleDataAt(status.Value, timeout);
+ }
+}

0 comments on commit 4a39cc1

Please sign in to comment.