public void DoesntRunIfTimerIsStartedAgain() { var startTime = new DateTime(); var autoResetEvent = new AutoResetEvent(false); var timer = new Timer<string>(a => { ThreadAssertion(DateTime.Now.Subtract(startTime), new GreaterThanOrEqualConstraint(TimeSpan.FromSeconds(.9))); autoResetEvent.Set(); }); startTime = DateTime.Now; timer.Start(.5, ""); Thread.Sleep(TimeSpan.FromSeconds(.4)); timer.Start(.5, ""); Assert.AreEqual(true, autoResetEvent.WaitOne(TimeSpan.FromSeconds(10), false)); }
public AutoSave(Delegate notify, double saveTime, Game1 game) { this.notify = notify; timer = new Timer(saveTime, game); timer.Elapsed = SaveDue; timer.Start(); }
public void AdoNetOnly() { for (int n = 2; n < 4000; n *= 2) { Simple[] simples = new Simple[n]; for (int i = 0; i < n; i++) { simples[i] = new Simple(); simples[i].Init(); simples[i].Count = i; simples[i].Id = i; } //Now do timings Timer timer = new Timer(); IDbConnection _connection = sqlMap.DataSource.DbProvider.CreateConnection(); _connection.ConnectionString = sqlMap.DataSource.ConnectionString; _connection.Open(); timer.Start(); DirectAdoNet(_connection, simples, n, "j1"); timer.Stop(); double adonet = 1000000 * (timer.Duration / (double)n); _connection.Close(); _connection.Open(); timer.Start(); DirectAdoNet(_connection, simples, n, "j2"); timer.Stop(); adonet += 1000000 * (timer.Duration / (double)n); _connection.Close(); _connection.Open(); timer.Start(); DirectAdoNet(_connection, simples, n, "j2"); timer.Stop(); adonet += 1000000 * (timer.Duration / (double)n); _connection.Close(); System.Console.Out.WriteLine("Objects: " + n + " Direct ADO.NET: " + adonet.ToString("F3")); } System.GC.Collect(); }
public void RunsAfterHalfASecond() { var startTime = new DateTime(); var autoResetEvent = new AutoResetEvent(false); var timer = new Timer<string>(a => { ThreadAssertion(DateTime.Now.Subtract(startTime), new GreaterThanConstraint(TimeSpan.FromSeconds(.5))); autoResetEvent.Set(); }); startTime = DateTime.Now; timer.Start(.5, ""); Assert.AreEqual(true, autoResetEvent.WaitOne(TimeSpan.FromSeconds(10), false)); }
public void InitModel() { #if THREADING_TIMER // Create an inferred delegate that invokes methods for the timer. _paddleTimerCB = paddleTimerCallback; // Create a timer that signals the delegate to invoke _paddleTimer = new System.Threading.Timer(_paddleTimerCB, null, 5, 5); #else _paddleTimer = new System.Timers.Timer(5); _paddleTimer.Elapsed += new ElapsedEventHandler(paddleTimerHandler); _paddleTimer.Start(); #endif // how far does the paddle move (pixels) _paddleMoveSize = 5; }
private void Setup(bool oneOff, int interval) { if (interval <= 0) { throw new Exception("Invalid timing interval or callback."); } Interval = interval; if (Callback == null && OnComplete == null) { throw new Exception("A callback must be provided for timer methods."); } OneOff = oneOff; #if UNITY_WP8 InternalTimer = new System.Threading.Timer(Elapsed, null, 0, interval); #elif UNITY_METRO MaxTime = (float)Interval / 1000f; #else InternalTimer = new System.Timers.Timer(); InternalTimer.Elapsed += Elapsed; InternalTimer.Interval = interval; InternalTimer.Start(); #endif if (!OneOff) { // Add to active set: if (FirstActive == null) { FirstActive = LastActive = this; } else { Previous = LastActive; LastActive = LastActive.Next = this; } } }
private void Setup(bool oneOff, int interval) { if (interval <= 0) { throw new Exception("Invalid timing interval or callback."); } if (Callback == null && OnComplete == null) { throw new Exception("A callback must be provided for timer methods."); } OneOff = oneOff; #if UNITY_WP8 InternalTimer = new System.Threading.Timer(Elapsed, null, 0, interval); #elif UNITY_METRO #else InternalTimer = new System.Timers.Timer(); InternalTimer.Elapsed += Elapsed; InternalTimer.Interval = interval; InternalTimer.Start(); #endif }
public override void Start(MessageTypes types) { base.Start (types); Timer timer = new Timer (100); timer = new Timer (100); timer.TimesUp += OnDeliveryTimer; timer.Start(); this.deliveryTimer = timer; }
public async Task<ClientConnectionResult> ConnectAsync (Target target, MessageTypes messageTypes) { if (target == null) throw new ArgumentNullException ("target"); if (!Enum.IsDefined (typeof (MessageTypes), messageTypes)) throw new ArgumentOutOfRangeException ("messageTypes"); IPEndPoint = await target.ToIPEndPointAsync().ConfigureAwait (false); if (IPEndPoint.AddressFamily != AddressFamily.InterNetwork && IPEndPoint.AddressFamily != AddressFamily.InterNetworkV6) throw new ArgumentException ("Unsupported endpoint AddressFamily"); var ntcs = new TaskCompletionSource<ClientConnectionResult>(); ThreadPool.QueueUserWorkItem (s => { Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Waiting for pending ({0}) async..", this.pendingAsync)); while (this.pendingAsync > 0 || Interlocked.CompareExchange (ref this.connectTcs, ntcs, null) != null) Thread.Sleep (0); int p = Interlocked.Increment (ref this.pendingAsync); Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Increment pending: {0}", p)); this.serializer = new ClientMessageSerializer (this, this.originalProtocols); IEnumerable<string> hashAlgs = null; if (this.localCrypto != null) hashAlgs = this.localCrypto.SupportedHashAlgs; Start (messageTypes); this.socket = this.listener.GetSocket (IPEndPoint); Timer dtimer = new Timer (100); dtimer.TimesUp += OnDeliveryTimer; dtimer.Start(); this.deliveryTimer = dtimer; Timer t = new Timer (30000); Timer previousTimer = Interlocked.Exchange (ref this.connectTimer, t); if (previousTimer != null) previousTimer.Dispose(); t.AutoReset = false; t.TimesUp += (sender, args) => { var tcs = this.connectTcs; if (tcs != null) tcs.TrySetResult (new ClientConnectionResult (ConnectionResult.ConnectionFailed, null)); Disconnect (ConnectionResult.ConnectionFailed); t.Dispose(); }; t.Start(); RemoteTarget = target; SendAsync (new ConnectMessage { Protocols = Protocols, SignatureHashAlgorithms = hashAlgs }).ContinueWith (st => { int pa = Interlocked.Decrement (ref this.pendingAsync); Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Decrement pending: {0}", pa)); }); }); return await ntcs.Task.ConfigureAwait (false); }
public void IbatisOnly() { for (int n = 2; n < 4000; n *= 2) { Simple[] simples = new Simple[n]; object[] ids = new object[n]; for (int i = 0; i < n; i++) { simples[i] = new Simple(); simples[i].Init(); simples[i].Count = i; simples[i].Id = i; } //Now do timings Timer timer = new Timer(); GC.Collect(); GC.WaitForPendingFinalizers(); sqlMap.OpenConnection(); timer.Start(); Ibatis(simples, n, "h1"); timer.Stop(); double ibatis = 1000000 * (timer.Duration / (double)n); sqlMap.CloseConnection(); sqlMap.OpenConnection(); timer.Start(); Ibatis(simples, n, "h2"); timer.Stop(); ibatis += 1000000 * (timer.Duration / (double)n); sqlMap.CloseConnection(); sqlMap.OpenConnection(); timer.Start(); Ibatis(simples, n, "h2"); timer.Stop(); ibatis += 1000000 * (timer.Duration / (double)n); sqlMap.CloseConnection(); System.Console.WriteLine("Objects: " + n + " - iBATIS DataMapper: " + ibatis.ToString("F3")); } System.GC.Collect(); }
public void Simultaneous() { double ibatis = 0; double adonet = 0; IDbConnection _connection = sqlMap.DataSource.DbProvider.CreateConnection(); _connection.ConnectionString = sqlMap.DataSource.ConnectionString; for (int n = 2; n < 4000; n *= 2) { Simple[] simples = new Simple[n]; for (int i = 0; i < n; i++) { simples[i] = new Simple(); simples[i].Init(); simples[i].Count = i; simples[i].Id = i; } sqlMap.OpenConnection(); Ibatis(simples, n, "h0"); sqlMap.CloseConnection(); _connection.Open(); DirectAdoNet(_connection, simples, n, "j0"); _connection.Close(); sqlMap.OpenConnection(); Ibatis(simples, n, "h0"); sqlMap.CloseConnection(); _connection.Open(); DirectAdoNet(_connection, simples, n, "j0"); _connection.Close(); //Now do timings Timer timer = new Timer(); GC.Collect(); GC.WaitForPendingFinalizers(); sqlMap.OpenConnection(); timer.Start(); Ibatis(simples, n, "h1"); timer.Stop(); ibatis = 1000000 * (timer.Duration / (double)n); sqlMap.CloseConnection(); _connection.Open(); timer.Start(); DirectAdoNet(_connection, simples, n, "j1"); timer.Stop(); adonet = 1000000 * (timer.Duration / (double)n); _connection.Close(); sqlMap.OpenConnection(); timer.Start(); Ibatis(simples, n, "h2"); timer.Stop(); ibatis += 1000000 * (timer.Duration / (double)n); sqlMap.CloseConnection(); _connection.Open(); timer.Start(); DirectAdoNet(_connection, simples, n, "j2"); timer.Stop(); adonet += 1000000 * (timer.Duration / (double)n); _connection.Close(); sqlMap.OpenConnection(); timer.Start(); Ibatis(simples, n, "h2"); timer.Stop(); ibatis += 1000000 * (timer.Duration / (double)n); sqlMap.CloseConnection(); _connection.Open(); timer.Start(); DirectAdoNet(_connection, simples, n, "j2"); timer.Stop(); adonet += 1000000 * (timer.Duration / (double)n); _connection.Close(); System.Console.Out.WriteLine("Objects " + n + " iBATIS DataMapper : " + ibatis.ToString("F3") + " / Direct ADO.NET: " + adonet.ToString("F3") + " Ratio: " + ((ibatis / adonet)).ToString("F3")); } System.GC.Collect(); }
public void Many() { double ibatis = 0; double adonet = 0; for (int n = 0; n < 5; n++) { Simple[] simples = new Simple[n]; for (int i = 0; i < n; i++) { simples[i] = new Simple(); simples[i].Init(); simples[i].Count = i; simples[i].Id = i; } sqlMap.OpenConnection(); Ibatis(simples, n, "h0"); sqlMap.CloseConnection(); IDbConnection _connection = sqlMap.DataSource.DbProvider.CreateConnection(); _connection.ConnectionString = sqlMap.DataSource.ConnectionString; _connection.Open(); DirectAdoNet(_connection, simples, n, "j0"); _connection.Close(); sqlMap.OpenConnection(); Ibatis(simples, n, "h0"); sqlMap.CloseConnection(); _connection.Open(); DirectAdoNet(_connection, simples, n, "j0"); _connection.Close(); // now do timings int loops = 30; Timer timer = new Timer(); for (int runIndex = 1; runIndex < 4; runIndex++) { GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < loops; i++) { sqlMap.OpenConnection(); Ibatis(simples, n, "h" + runIndex.ToString()); sqlMap.CloseConnection(); } timer.Stop(); ibatis += 1000000 * (timer.Duration / (double)loops); GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < loops; i++) { _connection.Open(); DirectAdoNet(_connection, simples, n, "j" + runIndex.ToString()); _connection.Close(); } timer.Stop(); adonet += 1000000 * (timer.Duration / (double)loops); } } System.Console.Out.WriteLine("iBatis DataMapper : " + ibatis.ToString("F3") + " / Direct ADO.NET: " + adonet.ToString("F3") + " Ratio: " + ((ibatis / adonet)).ToString("F3")); System.GC.Collect(); }
/// <summary> /// /// </summary> /// <param name="totalWork"> /// A <see cref="System.Int32"/> /// </param> /// <param name="title"> /// A <see cref="System.String"/> /// </param> /// <param name="barLength"> /// A <see cref="System.Int32"/> /// </param> public ProgressReporter(int totalWork, string title, int barLength) { plussesPrinted = 0; totalPlusses = barLength - title.Length; frequency = (double)totalWork / (double)totalPlusses; count = frequency; timer = new Timer (); timer.Start (); Console.Write (" > " + title + ": ["); left = Console.CursorLeft; right = left + totalPlusses - 1; Console.CursorLeft = right; Console.Write ("]"); Console.CursorLeft = left; }