public static void Main() { Console.WriteLine("Start new timer..."); // Remember start time var state = new StateData { InitialTickCount = Environment.TickCount }; // Short version FlexTimer.Factory.StartNew(() => Console.WriteLine(" --- ping!"), 1000); // Short typed version FlexTimer<double>.Factory.StartNew(d => Console.WriteLine(" --- double: " + d), 3.14, 1000); // Create a timer and pass 'state' object using (var timer = new FlexTimer<StateData>(SlowAction, state, 2000)) { // Prevent handler call if the previous call is still processed timer.EnableConcurrency = false; // MissedCallHandler will execute if a handler call has prevented due // a long time of processing previous one timer.SetMissedCallHandler(MissedCallHandler); timer.Start(TimeSpan.Zero); // Call a handler immediately Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } }
public void Test_ConcurrencyOn() { var test = new SleepAndSetTestEnvironment(); using (var timer = new FlexTimer<AutoResetEvent>(test.Action, new AutoResetEvent(false), 100)) { timer.EnableConcurrency = true; timer.SetMissedCallHandler(test.MissedCallHandler); timer.Start(); timer.State.WaitOne(); Console.WriteLine($"[ConcurrencyOn] Counter: {test.Counter}, MaxConcurrent: {test.MaxConcurrent}, " + $"MissedCalls: {test.MissedCalls}"); Assert.True(test.MaxConcurrent > 1); Assert.True(test.MissedCalls == 0); } }
public void MissedCallHandler(FlexTimer<AutoResetEvent> timer) { Interlocked.Increment(ref MissedCalls); Console.WriteLine(" -> trace: missed call"); }
private static void MissedCallHandler(FlexTimer<StateData> timer) { // We can obtain a state data via the State property timer.State.Print("Missed call detected!"); }