This commit is contained in:
		
							parent
							
								
									ef267ef186
								
							
						
					
					
						commit
						1e9d37ac13
					
				
					 1 changed files with 38 additions and 15 deletions
				
			
		|  | @ -19,6 +19,8 @@ namespace SafeExamBrowser.SystemComponents.Registry | |||
| { | ||||
| 	public class Registry : IRegistry | ||||
| 	{ | ||||
| 		private const int ONE_SECOND = 1000; | ||||
| 
 | ||||
| 		private readonly ILogger logger; | ||||
| 		private readonly ConcurrentDictionary<(string key, string name), object> values; | ||||
| 
 | ||||
|  | @ -34,8 +36,6 @@ namespace SafeExamBrowser.SystemComponents.Registry | |||
| 
 | ||||
| 		public void StartMonitoring(string key, string name) | ||||
| 		{ | ||||
| 			const int ONE_SECOND = 1000; | ||||
| 
 | ||||
| 			if (timer?.Enabled != true) | ||||
| 			{ | ||||
| 				timer = new Timer(ONE_SECOND); | ||||
|  | @ -44,14 +44,17 @@ namespace SafeExamBrowser.SystemComponents.Registry | |||
| 				timer.Start(); | ||||
| 			} | ||||
| 
 | ||||
| 			if (TryRead(key, name, out var value)) | ||||
| 			{ | ||||
| 			var success = TryRead(key, name, out var value); | ||||
| 
 | ||||
| 			values.TryAdd((key, name), value); | ||||
| 
 | ||||
| 			if (success) | ||||
| 			{ | ||||
| 				logger.Debug($"Started monitoring value '{name}' from registry key '{key}'. Initial value: '{value}'."); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				logger.Error($"Failed to start monitoring value '{name}' from registry key '{key}'!"); | ||||
| 				logger.Debug($"Started monitoring value '{name}' from registry key '{key}'. Value does currently not exist or initial read failed."); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
|  | @ -86,7 +89,7 @@ namespace SafeExamBrowser.SystemComponents.Registry | |||
| 				logger.Error($"Failed to read value '{name}' from registry key '{key}'!", e); | ||||
| 			} | ||||
| 
 | ||||
| 			return value != default && !ReferenceEquals(value, defaultValue); | ||||
| 			return value != default && value != defaultValue; | ||||
| 		} | ||||
| 
 | ||||
| 		public bool TryGetNames(string keyName, out IEnumerable<string> names) | ||||
|  | @ -141,9 +144,28 @@ namespace SafeExamBrowser.SystemComponents.Registry | |||
| 			return subKeys != default; | ||||
| 		} | ||||
| 
 | ||||
| 		private bool Exists(string key, string name) | ||||
| 		{ | ||||
| 			var defaultValue = new object(); | ||||
| 			var value = default(object); | ||||
| 
 | ||||
| 			try | ||||
| 			{ | ||||
| 				value = Microsoft.Win32.Registry.GetValue(key, name, defaultValue); | ||||
| 			} | ||||
| 			catch (Exception e) | ||||
| 			{ | ||||
| 				logger.Error($"Failed to read value '{name}' from registry key '{key}'!", e); | ||||
| 			} | ||||
| 
 | ||||
| 			return value != default && value != defaultValue; | ||||
| 		} | ||||
| 
 | ||||
| 		private void Timer_Elapsed(object sender, ElapsedEventArgs e) | ||||
| 		{ | ||||
| 			foreach (var item in values) | ||||
| 			{ | ||||
| 				if (Exists(item.Key.key, item.Key.name)) | ||||
| 				{ | ||||
| 					if (TryRead(item.Key.key, item.Key.name, out var value)) | ||||
| 					{ | ||||
|  | @ -159,6 +181,7 @@ namespace SafeExamBrowser.SystemComponents.Registry | |||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		private bool TryOpenKey(string keyName, out RegistryKey key) | ||||
| 		{ | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Damian Büchel
						Damian Büchel