private void AuthenticationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { List <ApiKey> authenticatedKeys = null; List <ApiKey> unauthenticatedKeys = null; lock (_lock) { authenticatedKeys = AuthenticatedKeys.ToList(); unauthenticatedKeys = UnauthenticatedKeys.ToList(); } if (authenticatedKeys != null && unauthenticatedKeys != null) { // Check to make sure each Authenticated key is still valid foreach (var key in authenticatedKeys) { // Create new Device bool success = Device.Create(key.Key, key.DeviceId, Configuration.AuthenticationUrl); if (!success) { lock (_lock) { // Remove from Authenticated list int i = AuthenticatedKeys.ToList().FindIndex(o => o.Key == key.Key && o.DeviceId == key.DeviceId); if (i >= 0) { AuthenticatedKeys.RemoveAt(i); } // Add to Unauthenticated list UnauthenticatedKeys.Add(new ApiKey(key.Key, key.DeviceId)); } } } // Check each Unauthenticated key if now valid foreach (var key in unauthenticatedKeys) { // Create new Device bool success = Device.Create(key.Key, key.DeviceId, Configuration.AuthenticationUrl); if (success) { lock (_lock) { // Remove from Unauthenticated list int i = UnauthenticatedKeys.ToList().FindIndex(o => o.Key == key.Key && o.DeviceId == key.DeviceId); if (i >= 0) { UnauthenticatedKeys.RemoveAt(i); } // Add to Authenticated list AuthenticatedKeys.Add(new ApiKey(key.Key, key.DeviceId)); } } } } }
private static void HarvestData(Object source, System.Timers.ElapsedEventArgs eea) { Console.WriteLine($"triggered at: {eea.SignalTime:HH:mm:ss.fff}"); DoHarvest(); }