public void ConcurrentTimers() { const int NumConcurrentTimers = 10; var timersSignaled = new ConcurrentDictionary <IntPtr, bool>(); int numSignaled = 0; var elapsed = new ManualResetEventSlim(); TimerElapsedEventHandler handler = (sender, args) => { // A timer might fire more than once. When it fires the first time, track it by adding // it to a set and then increment the number of timers that have ever fired. When all // timers have fired, set the event. if (timersSignaled.TryAdd(args.TimerId, true) && Interlocked.Increment(ref numSignaled) == NumConcurrentTimers) { elapsed.Set(); } }; SystemEvents.TimerElapsed += handler; try { if (PlatformDetection.IsFullFramework) { // netfx has a bug where it will allow EnsureSystemEvents to proceed without actually creating the HWND SystemEventsTest.WaitForSystemEventsWindow(); } // Create all the timers var timers = new List <IntPtr>(); for (int i = 0; i < NumConcurrentTimers; i++) { timers.Add(SystemEvents.CreateTimer(TimerInterval)); } // Wait for them all to fire Assert.True(elapsed.Wait(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); // Delete them all foreach (IntPtr timer in timers) { Assert.True(timersSignaled.TryGetValue(timer, out _)); SystemEvents.KillTimer(timer); } } finally { SystemEvents.TimerElapsed -= handler; elapsed.Dispose(); } }
public void ConcurrentTimers() { const int NumConcurrentTimers = 10; var timersSignalled = new Dictionary <IntPtr, bool>(); int numSignaled = 0; var elapsed = new AutoResetEvent(false); TimerElapsedEventHandler handler = (sender, args) => { bool signaled = false; if (timersSignalled.TryGetValue(args.TimerId, out signaled) && !signaled) { timersSignalled[args.TimerId] = true; if (Interlocked.Increment(ref numSignaled) == NumConcurrentTimers) { elapsed.Set(); } } }; SystemEvents.TimerElapsed += handler; try { if (PlatformDetection.IsFullFramework) { // desktop has a bug where it will allow EnsureSystemEvents to proceed without actually creating the HWND SystemEventsTest.WaitForSystemEventsWindow(); } for (int i = 0; i < NumConcurrentTimers; i++) { timersSignalled[SystemEvents.CreateTimer(TimerInterval)] = false; } Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); foreach (var timer in timersSignalled.Keys.ToArray()) { Assert.True(timersSignalled[timer]); SystemEvents.KillTimer(timer); } } finally { SystemEvents.TimerElapsed -= handler; elapsed.Dispose(); } }
public void Methods_Deny_Unrestricted() { IntPtr timer = SystemEvents.CreateTimer(5000); SystemEvents.KillTimer(timer); try { SystemEvents.InvokeOnEventsThread(new TimerElapsedEventHandler(TimerCallback)); } catch (NotImplementedException) { // mono } }
public void TimerElapsedSignaled() { var elapsed = new AutoResetEvent(false); object elapsedSender = null; IntPtr timer = IntPtr.Zero; TimerElapsedEventHandler handler = (sender, args) => { if (args?.TimerId == timer) { elapsedSender = sender; elapsed.Set(); } }; SystemEvents.TimerElapsed += handler; try { if (PlatformDetection.IsFullFramework) { // desktop has a bug where it will allow EnsureSystemEvents to proceed without actually creating the HWND SystemEventsTest.WaitForSystemEventsWindow(); } timer = SystemEvents.CreateTimer(TimerInterval); Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); Assert.IsType <SystemEvents>(elapsedSender); // Timer should fire more than once Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); SystemEvents.KillTimer(timer); elapsed.Reset(); // Timer should not fire once killed Assert.False(elapsed.WaitOne(TimerInterval * SystemEventsTest.UnexpectedEventMultiplier)); } finally { SystemEvents.TimerElapsed -= handler; elapsed.Dispose(); } }
public void ConcurrentTimers() { const int NumConcurrentTimers = 10; var timersSignalled = new Dictionary <IntPtr, bool>(); int numSignaled = 0; var elapsed = new AutoResetEvent(false); TimerElapsedEventHandler handler = (sender, args) => { bool signaled = false; if (timersSignalled.TryGetValue(args.TimerId, out signaled) && !signaled) { timersSignalled[args.TimerId] = true; if (Interlocked.Increment(ref numSignaled) == NumConcurrentTimers) { elapsed.Set(); } } }; SystemEvents.TimerElapsed += handler; try { for (int i = 0; i < NumConcurrentTimers; i++) { timersSignalled[SystemEvents.CreateTimer(TimerInterval)] = false; } Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); foreach (var timer in timersSignalled.Keys.ToArray()) { Assert.True(timersSignalled[timer]); SystemEvents.KillTimer(timer); } } finally { SystemEvents.TimerElapsed -= handler; elapsed.Dispose(); } }
public void TimerElapsedSignaled() { var elapsed = new AutoResetEvent(false); object elapsedSender = null; IntPtr timer = IntPtr.Zero; TimerElapsedEventHandler handler = (sender, args) => { if (args?.TimerId == timer) { elapsedSender = sender; elapsed.Set(); } }; SystemEvents.TimerElapsed += handler; try { timer = SystemEvents.CreateTimer(TimerInterval); Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); Assert.IsType <SystemEvents>(elapsedSender); // Timer should fire more than once Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier)); SystemEvents.KillTimer(timer); elapsed.Reset(); // Timer should not fire once killed Assert.False(elapsed.WaitOne(TimerInterval * SystemEventsTest.UnexpectedEventMultiplier)); } finally { SystemEvents.TimerElapsed -= handler; elapsed.Dispose(); } }
public void CreateTimerInvalidInterval() { Assert.Throws <ArgumentException>(() => SystemEvents.CreateTimer(0)); Assert.Throws <ArgumentException>(() => SystemEvents.CreateTimer(-1)); Assert.Throws <ArgumentException>(() => SystemEvents.CreateTimer(int.MinValue)); }